/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode reversedHead=null;
ListNode currentNode=head;
ListNode preNode=null;
while(currentNode!=null){
ListNode temp=currentNode.next;
if(temp==null)
reversedHead=temp;//尾结点
currentNode.next=preNode;
preNode=currentNode;
currentNode=temp;
}
return preNode;
}
}
面试题16:反转链表
最新推荐文章于 2021-12-08 19:13:29 发布