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

翻转链表

时间:2017-05-02 19:46:47      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:单链表   双向链表   需要   tno   翻转   div   last   reverse   ati   

/**
     * 翻转双向链表
     * 需要记录翻转后的下一个节点
     * @param head
     * @return
     */
    public DoubleNode reverserList(DoubleNode head){
        DoubleNode pre = null;
        DoubleNode next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            head.last = next;
            pre = head;
            head = next;
        }
        return pre;
    }
/**
     * 翻转单链表
     * 需要记录翻转后的下一个节点
     * @param head
     * @return
     */
    public static ListNode reverse2(ListNode head){
        ListNode pre = null;
        ListNode next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

 

翻转链表

标签:单链表   双向链表   需要   tno   翻转   div   last   reverse   ati   

原文地址:http://www.cnblogs.com/caobojia/p/6797537.html

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