Blind 75 - Reverse LinkedList
Similar to swapping two numbers. Start with prev as null and current and keep swapping next. Return prev because that's the last value.
Given a LinkedList, reverse it in place.
Approaches
O(n) time. O(1) space.
Similar to swapping two numbers. Start with prev as null and current and keep swapping next. Return prev because that’s the last value.
[image]
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode current = head;
while(current!=null){
ListNode tempCurrent = current.next;
current.next = prev;
prev = current;
current = tempCurrent;
}
return prev;
}