[LeetCode] 206. 反转链表

自己做的:

public static ListNode reverseList(ListNode head) {
        ListNode cur=head;
//        ListNode next=head.next;
        ListNode pre=null;
        while(cur.next!=null){
            ListNode next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        return cur;
    }

没有正确输出

while循环中不应该为:cur。next!=null   并且最后返回也不应该返回cur而是pre

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode cur=head;
        ListNode pre=null;
        ListNode next=null;
        while(cur!=null){
            next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }
}
原文地址:https://www.cnblogs.com/doyi111/p/12667659.html