CJCoding With Joseph
15free / daySign inCreate account

Reverse the List In Place

Reversing means re-aiming every next pointer at the node behind it instead of building a second list. The three cursors are already declared for you; the loop is yours.

Requirements:
1. reverse() flips the order of the existing nodes without creating any new Node.
2. Save the next node before you overwrite current.next, or the rest of the chain is lost.
3. When the loop ends, head must point at what used to be the last node. An empty or one-node list must survive unchanged.

The Node class is provided - do not rename it and do not add public to it, because a Java source file may contain only one public class.

Do NOT write a main method.

Expected Output:

10 -> 20 -> 30 -> 40 -> null
40 -> 30 -> 20 -> 10 -> null
4
10 -> 20 -> 30 -> 40 -> null

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsLinked Lists
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.