码迷,mamicode.com
首页 > 其他好文 > 详细

206. Reverse Linked List

时间:2016-06-09 13:25:55      阅读:112      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!