您的位置:首页 > 编程语言 > Go语言

LeetCode 206. Reverse Linked List

2017-04-17 00:29 381 查看
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 || head.next==null)
return head;
else{
ListNode p=head.next;
ListNode q=p.next;
ListNode tail=head;
tail.next=null;
while(q!=null){
p.next=tail;
tail=p;
p=q;
q=q.next;
}
p.next=tail;
return p;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Algorithm leetcode