1->2->3 ==> 3->2->1
1. Create a temporary previous ListNode
and initialize it to null
.
2. Traverse through the list of nodes till the head node
becomes null.
3. While traversing at each step, save the head.next i.e node next to the head node in a temporary front
node i.e. ListNode front = head.next;
4. Track a node just behind the head node in a temporary node previous
5. Make the head's next link point to previous node i.e. head.next = previous
6. Move the temporary node previous
one step ahead in the list
by assigning the head node to the previous node i.e. previous = head
7. Move the head node one step ahead in the list by assigning the front node to the head node i.e. head = front
8. Return the previous node
i.e. the new head at the end of the loop.
public ListNode reverseList(ListNode head) { }
C
Java
Python