class Solution {
public ListNode reverseList(ListNode head) {
// these variables are references, not copies of the node itself
// so we are not using them as copies of the nodes, but as references (pointers)
ListNode previous = null;
ListNode current = head;
ListNode next = null;
while (current != null) {
next = current.next; // save next
current.next = previous; // reverse
// advance previous & current
previous = current; // previous points to the old current
current = next; // current points to the old next
}
return previous; // new head at the end
}
}
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)