206. Reverse Linked List
problem solving ideas
- Define three pointers, cur pre temp
- cur points to the current node, then pre points to the node before cur, and temp saves the next node of cur
- Because you need to change the direction of cur, you need temp
- Finally return pre is to return the new head node
class Solution {
public ListNode reverseList(ListNode head) {
ListNode temp;
ListNode pre = null;
ListNode cur = head;
while(cur != null){
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}