public ListNode reverseList(ListNode head){
if(headnull){
return null;
}
if(head.nextnull){
return head;
}
ListNode newhead=null;
ListNode cur=head;
ListNode prev=null;
while(cur!=null){
ListNode next=cur.next;
if(next==null){
newhead=cur;
}
cur.next=prev;
//更新三个位置
prev=cur;
cur=next;
}
return newhead;
}
反转一个单链表。
最新推荐文章于 2022-04-22 23:21:42 发布