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.
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...
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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F3702254c-4a7b-44d2-9c66-70d7bfaf3f8a%2F35e91da3-e973-4172-b12f-6ad5478ef881%2Fe9szvc_processed.png&w=3840&q=75)
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Recommended textbooks for you
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
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…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
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)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
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…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
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)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
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](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY