Sample Input Sample Output Explanation x-> 10 -> 20 -> 30 -> 40 -> |x-> 20 -> 10 -> 30 -> 40 -> 50 -> 60 -> 70-> 80 -> null | 60 -> 50 -> 70 -> 80 -> null reversed if the kth node value is even. Here, as k is 2, every 2 alternate nodes are First two nodes are reversed: 10 -> 20 is changed | to 20 -> 10 because 10 is even. |Then the next 2 nodes are skipped. Again, the next two nodes are reversed 50 -> 60 is changed to 60 -> 50 because 50 is even. Then the next 2 nodes are skipped. k = 2 x-> 11 -> 22 -> 33 -> 44 -> x -> 11 -> 22 -> 33 -> 44 -> Here, as k is 3, every 3 alternate nodes are 55 -> 66 -> 78-> 88 -> null 55 -> 66 -> 88 -> 78 -> null reversed if the kth node value is even. First three nodes are not reversed: 11 -> 22 -> 33 because the value 11 is odd. Then the next 3 nodes are skipped. Again, next three nodes are reversed, as there are only two nodes, those will be reversed, that is, 78 -> 8 is changed to 88 -> 78 as 78 is even. k = 3 x -> null k = 5 "No list" As the given list is empty, it will return “No list". x -> 10 -> 20 -> 30 -> null LengthException k = 5 As the given list size is less than the k value, it will throw a LengthException.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

IN PYTHON

Given a dummy-headed singly linked list, write a method that reverses the given list
every k nodes only if the kth node contains an even value. If k is greater than the size of
the list or negative, then throw LengthException. If the given list is empty returns "No
list".
[Note: you are not allowed to create a new list. You are allowed to use only range(),
print() and input()].
Sample Input
Sample Output
Explanation
x-> 10 -> 20 -> 30 -> 40 -> x-> 20 -> 10 -> 30 -> 40 ->
50 -> 60 -> 70-> 80 -> null 60 -> 50 -> 70 -> 80 -> null reversed if the kth node value is even.
Here, as k is 2, every 2 alternate nodes are
First two nodes are reversed: 10 -> 20 is changed
to 20 -> 10 because 10 is even.
k = 2
Then the next 2 nodes are skipped.
Again, the next two nodes are reversed 50 -> 60
is changed to 60 -> 50 because 50 is even.
Then the next 2 nodes are skipped.
x-> 11 -> 22 -> 33 -> 44 -> x -> 11 -> 22 -> 33 -> 44 -> Here, as k is 3, every 3 alternate nodes are
55 -> 66 -> 78-> 88 -> null 55 -> 66 -> 88 -> 78 -> null reversed if the kth node value is even.
First three nodes are not reversed: 11-> 22 -> 33
because the value 11 is odd.
Then the next 3 nodes are skipped.
Again, next three nodes are reversed, as there are
only two nodes, those will be reversed, that is, 78
-> 88 is changed to 88 -> 78 as 78 is even.
k = 3
"No list"
x -> null
k = 5
As the given list is empty, it will return "No list".
x -> 10 -> 20 -> 30 -> null LengthException
k = 5
As the given list size is less than the k value, it
will throw a LengthException.
Transcribed Image Text:Given a dummy-headed singly linked list, write a method that reverses the given list every k nodes only if the kth node contains an even value. If k is greater than the size of the list or negative, then throw LengthException. If the given list is empty returns "No list". [Note: you are not allowed to create a new list. You are allowed to use only range(), print() and input()]. Sample Input Sample Output Explanation x-> 10 -> 20 -> 30 -> 40 -> x-> 20 -> 10 -> 30 -> 40 -> 50 -> 60 -> 70-> 80 -> null 60 -> 50 -> 70 -> 80 -> null reversed if the kth node value is even. Here, as k is 2, every 2 alternate nodes are First two nodes are reversed: 10 -> 20 is changed to 20 -> 10 because 10 is even. k = 2 Then the next 2 nodes are skipped. Again, the next two nodes are reversed 50 -> 60 is changed to 60 -> 50 because 50 is even. Then the next 2 nodes are skipped. x-> 11 -> 22 -> 33 -> 44 -> x -> 11 -> 22 -> 33 -> 44 -> Here, as k is 3, every 3 alternate nodes are 55 -> 66 -> 78-> 88 -> null 55 -> 66 -> 88 -> 78 -> null reversed if the kth node value is even. First three nodes are not reversed: 11-> 22 -> 33 because the value 11 is odd. Then the next 3 nodes are skipped. Again, next three nodes are reversed, as there are only two nodes, those will be reversed, that is, 78 -> 88 is changed to 88 -> 78 as 78 is even. k = 3 "No list" x -> null k = 5 As the given list is empty, it will return "No list". x -> 10 -> 20 -> 30 -> null LengthException k = 5 As the given list size is less than the k value, it will throw a LengthException.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY