反转单链思路简单,但是需要仔细实现,不能遗漏细节,不自己独立写一遍永远不知道自己错在哪。
public static ListNode reverseList(ListNode head) {
if(head==
null) return
null;
ListNode pre=
null;
ListNode cur=head;
ListNode
next=head.
next;
cur.
next=pre;
while(
next!=
null){
pre=cur;
cur=
next;
next=
next.
next;
cur.
next=pre;
}
return cur;
}
注意:此类方法会改变原链表结果,即原head对象在调用此方法后会变为尾结点,它的next指针会指向null。
转载请注明原文地址: https://ju.6miu.com/read-678868.html