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

单链表翻转

时间:2015-07-05 18:30:27      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

    /*
     * three argvs: headNext(the head‘s next), tmp(move forward), node(the removed ListNode).
    */    
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode headNext = head.next;
        ListNode tmp = headNext;
        while (tmp.next != null) {
            ListNode node = tmp.next;
            tmp.next = node.next;
            head.next = node;
            node.next = headNext;
            
            headNext = head.next;
        }
        ListNode newHead = head.next;
        head.next = null;
        tmp.next = head;
        return newHead;
    }    

 

单链表翻转

标签:

原文地址:http://www.cnblogs.com/lasclocker/p/4622643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!