Given a singly linked list, you need to do two tasks. Swap the 2nd node with the last node. Then replace a new node (node element will be given as function/method parameter) in the middle of the linked list only if the node value is odd, |otherwise insert it after the middle node. For example, if the given linked list is 1->2->8->4->5, and an element is given 10 (be careful it is data/element, not the Node) then the linked list should be modified to 1->5->8->10->4->2. Because 2 and 5 will be swapped and 8 is even, so 10 will be inserted after 8. If there are even nodes, then there would be two middle nodes, you need to replace the first middle element. If the input linked list is NULL, then it should remain NULL. If the input linked list has 1 node and contains an even value, then a new node should be placed after the even node otherwise no change will be applied. Sample 1: Input: 8->2->3->4->5->6, 12 output: 8->6->12->4->5->2 Sample 2: Input: NULL, 10 output: NULL Sample 3: Input: 1, 11 output: 1 Sample 4: Input: 1->2, 7 output: 7->2 Sample 5: Input: 1->2->3->4->5, 10 output: 1->5->10->4->2. Sample 6: Input: 2, 12 output: 2->12

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
100%

Assuming the node class has already been created, start your code from the method:

def swapReplaceMidOdd(head, element):

#write your code here

Given a singly linked list, you need to do two tasks.
Swap the 2nd node with the last node. Then replace a new node (node element will be given
as function/method parameter) in the middle of the linked list only if the node value is odd,
|otherwise insert it after the middle node.
For example, if the given linked list is 1->2->8->4->5, and an element is given 10 (be careful it is
data/element, not the Node) then the linked list should be modified to 1->5->8->10->4->2.
Because 2 and 5 will be swapped and 8 is even, so 10 will be inserted after 8.
If there are even nodes, then there would be two middle nodes, you need to replace the first
middle element.
If the input linked list is NULL, then it should remain NULL. If the input linked list has 1 node
and contains an even value, then a new node should be placed after the even node otherwise no
change will be applied.
Sample 1: Input: 8->2->3->4->5->6, 12 output: 8->6->12->4->5->2
Sample 2: Input: NULL, 10 output: NULL
Sample 3: Input: 1, 11 output: 1
Sample 4: Input: 1->2, 7 output: 7->2
Sample 5: Input: 1->2->3->4->5, 10 output: 1->5->10->4->2.
Sample 6: Input: 2, 12 output: 2->12
Transcribed Image Text:Given a singly linked list, you need to do two tasks. Swap the 2nd node with the last node. Then replace a new node (node element will be given as function/method parameter) in the middle of the linked list only if the node value is odd, |otherwise insert it after the middle node. For example, if the given linked list is 1->2->8->4->5, and an element is given 10 (be careful it is data/element, not the Node) then the linked list should be modified to 1->5->8->10->4->2. Because 2 and 5 will be swapped and 8 is even, so 10 will be inserted after 8. If there are even nodes, then there would be two middle nodes, you need to replace the first middle element. If the input linked list is NULL, then it should remain NULL. If the input linked list has 1 node and contains an even value, then a new node should be placed after the even node otherwise no change will be applied. Sample 1: Input: 8->2->3->4->5->6, 12 output: 8->6->12->4->5->2 Sample 2: Input: NULL, 10 output: NULL Sample 3: Input: 1, 11 output: 1 Sample 4: Input: 1->2, 7 output: 7->2 Sample 5: Input: 1->2->3->4->5, 10 output: 1->5->10->4->2. Sample 6: Input: 2, 12 output: 2->12
Expert Solution
steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Operations of Linked List
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