- Always examine if the node is null before you call the next field.
- Carefully define the end conditions of your loop.
- complexity time: O(N) space O(1)
-
dummy head represents head of reversed list, for each current node, insert it in front of reversed list dummy head, and re-position reversed list dummy head, then move current node pointer to its next
-
point newhead at current node to be swapped
-
assign previous newhead to newhead.next
-
move cur to cur.next
newhead, newhead.next, cur = cur, newhead, cur.next
step0 1, 2, 3, 4 newhead cur
step1 1, 2, 3, 4 newhead cur
step2 2, 1, 3, 4 newhead cur
step3: 3, 2, 1, 4 newhead cur
use one pointer curr to traverse from head to end (.next is null), when curr moves n steps, then start another pointer p1 at head, to move lock step with curr, when curr reaches end, p1 will be at n steps from end