Reverse Linked List

Reverse a singly linked list.

分析

做链表题的时候,一个最大的问题是常常迷惑指针终结在哪里? 是否需要临时的辅助变量?

怎么解决这个问题?

拿个具体例子画图走一遍

代码

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;

        while( cur!= null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;

        }
        return pre;
    }
}

results matching ""

    No results matching ""