您的位置:首页 > 其它

Leetcode题解 206. Reverse Linked List

2016-06-18 15:23 274 查看
Reverse a singly linked list.

用三个节点保存前、中、后三个节点即可。

/**
* Definition for singly-linked list.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null){
return null;
}else if(head.next==null){
return head;
}
ListNode prev,last;
prev=head;
head=head.next;
prev.next=null;
if(head.next==null){
head.next=prev;
}else{
last=head.next;
while(last!=null){
head.next=prev;
prev=head;
head=last;
last=last.next;
}
head.next=prev;
}
return head;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode