CJCoding With Joseph
15free / daySign inCreate account

Reverse a Singly Linked List In Place

Read an integer n, then n more integers, and build a singly linked list holding those values in the order they were read (the first value read is the head). Print the list, then reverse it IN PLACE by re-pointing each node's next pointer — do not build a second list and do not copy values between nodes. Print the reversed list, then free it.

For the input:

4
4 1 7 2

the program prints exactly the output shown in Expected Output.

Format: each line is the label, a colon, and then a space before every value. Nothing follows the last value.

Free the memory you allocate before the program ends — this question checks for memory leaks.

Expected Output:

Original: 4 1 7 2
Reversed: 2 7 1 4

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

Hints4 available
Topics:
Pointers
📥 Auto-Input:
4\n4 1 7 2\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (p, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.