Given a singly linked list, you need to do two tasks. Swap the first node with the last node. Then count the number of nodes and if the number of nodes is an odd number then delete the first node, otherwise delete the last node of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 2->3->4->1. Because 1 and 5 will be swapped and 5 will be deleted as the number of nodes in this linked list is 5 which is an odd number, that means the first node which contains 5 has been deleted. If the input linked list is NULL, then it should remain NULL. If the input linked list has 1 node, then this node should be deleted and a new head should be returned. Sample 1: Input: NULL output: NULL. Sample 2: Input: 1 output: NULL Sample 3: Input: 1->2 output: 2 Sample 4: Input: 1->2->3 output: 2->1 Sample 5: Input: 1->2->3->4_output: 4->2->3 Sample 6: Input: 1->2->3->4->5->6 output: 6->2->3->4->5. Input: The function takes one argument as input, the reference pointer to the head of the linked list. Output: Your function should return the head of the modified linked list. If the linked list is empty then it should return NULL.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Question
Given a singly linked list, you need to do two tasks.
Swap the first node with the last node. Then count the number of nodes and if the number of
nodes is an odd number then delete the first node, otherwise delete the last node of the linked
list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified
to 2->3->4->1. Because 1 and 5 will be swapped and 5 will be deleted as the number of nodes in
this linked list is 5 which is an odd number, that means the first node which contains 5 has been
deleted.
If the input linked list is NULL, then it should remain NULL. If the input linked list has 1 node,
then this node should be deleted and a new head should be returned.
Sample 1: Input: NULL output: NULL.
Sample 2: Input: 1 output: NULL
Sample 3: Input: 1->2 output: 2
Sample 4: Input: 1->2->3 output: 2->1
Sample 5: Input: 1->2->3->4 _output: 4->2->3
Sample 6: Input: 1->2->3->4->5->6 output: 6->2->3->4->5.
Input: The function takes one argument as input, the reference pointer to the head of the linked
list.
Output: Your function should return the head of the modified linked list. If the linked list is empty
then it should return NULL.
Transcribed Image Text:Question Given a singly linked list, you need to do two tasks. Swap the first node with the last node. Then count the number of nodes and if the number of nodes is an odd number then delete the first node, otherwise delete the last node of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 2->3->4->1. Because 1 and 5 will be swapped and 5 will be deleted as the number of nodes in this linked list is 5 which is an odd number, that means the first node which contains 5 has been deleted. If the input linked list is NULL, then it should remain NULL. If the input linked list has 1 node, then this node should be deleted and a new head should be returned. Sample 1: Input: NULL output: NULL. Sample 2: Input: 1 output: NULL Sample 3: Input: 1->2 output: 2 Sample 4: Input: 1->2->3 output: 2->1 Sample 5: Input: 1->2->3->4 _output: 4->2->3 Sample 6: Input: 1->2->3->4->5->6 output: 6->2->3->4->5. Input: The function takes one argument as input, the reference pointer to the head of the linked list. Output: Your function should return the head of the modified linked list. If the linked list is empty then it should return NULL.
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education