首页 > 其他 > 详细

leetcode-206

时间:2017-03-12 15:06:19      阅读:142      评论:0      收藏:0      [点我收藏+]

Reverse Linked List

Reverse a singly linked list.

此题不难,附上java代码:

/**
 * 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=head.next;
        pre.next=null;
        ListNode nxt;
        while(p!=null){
            nxt=p.next;
            p.next=pre;
            pre=p;
            p=nxt;
        }
        return pre;
    }
}

  

 

leetcode-206

原文:http://www.cnblogs.com/lcbg/p/6537530.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!