/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/ 先定义一个cur代表头节点,保存cur的下一个节点,让cur指向一个为空的引用,然后让空的引用等于cur,cur等于temp,相当于把这两个引用都后移了一次,这样下次循环的时候cur.next就指向了原来的节点
class Solution {
public ListNode reverseList(ListNode head) {
ListNode cur=head;
ListNode pre=null;
while(cur!=null){
ListNode temp=cur.next;
cur.next=pre;
pre=cur;
cur=temp;
}
return pre;
}
}
08-26
208
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-09
183
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交