首页 > 其他 > 详细

206. Reverse Linked List

时间:2016-06-09 13:23:38      阅读:390      评论:0      收藏:0      [点我收藏+]
    /*
     * 206. Reverse Linked List 
     * 2016-6-8 by Mingyang
     * 首先我的代码比较长,另外,刚开始做的时候固始思维,把pre设为了假头
     * 殊不知这里不需要假头,所以我们的pre只是一个null就好了再继续走
     * 另外,第一个解法更简便
     */
    // Brink‘s solution:
    public static ListNode reverseList(ListNode head) {
        ListNode prev = null;
        while (head != null) {
            ListNode temp = head.next;
            head.next = prev;
            prev = head;
            head = temp;
        }
        return prev;
    }
    //My solution
     public static ListNode reverseList1(ListNode head) {
            if(head==null)
             return null;
            ListNode run=head;
            ListNode pre=null;
            if(run.next==null)
             return run;
            while(run.next!=null){
                ListNode temp=run.next;
                run.next=pre;
                pre=run;
                run=temp;
            }
            run.next=pre;
            return run;
        }

 

206. Reverse Linked List

原文:http://www.cnblogs.com/zmyvszk/p/5572218.html

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