您的位置:首页 > 其它

leetcode 206. Reverse Linked List

2016-03-25 16:15 99 查看

leetcode 206. Reverse Linked List

题目

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;
}

ListNode pre=head;
ListNode p=pre.next;
pre.next=null;
ListNode nxt=null;

while(p!=null){
nxt=p.next;
p.next=pre;
pre=p;
p=nxt;
}

return pre;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: