首页 > 其他 > 详细

LeetCode——Reverse Linked List

时间:2015-05-20 23:59:37      阅读:384      评论:0      收藏:0      [点我收藏+]

         反转链表,用了一个比较笨的方法。

       

public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null)
            return head;
         ArrayList<Integer> list = new ArrayList<Integer>();
        ListNode p = head;
        while(p!=null) {
            list.add(p.val);
            p = p.next;
        }
        ListNode q = head;
        for(int i=list.size()-1; i>=0; i--) {
            q.val = list.get(i);
            q = q.next;
        }
        return head;
        
    }
}

 

LeetCode——Reverse Linked List

原文:http://www.cnblogs.com/wxisme/p/4518554.html

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