目的就是当前节点,指向前一个节点(prev),注意要保持当前节点的下一个节点(tmpNext),依次向后更新prev、curr,最后返回新的链表起始位置
public ListNode reverseList(ListNode head) {
ListNode curr = head;
ListNode prev = null;
while (curr != null) {
ListNode tmpNext = curr.next;
curr.next = prev;
prev = curr;
curr = tmpNext;
}
return prev;
}
网友评论