翻转链表

Posted 起个po名真费劲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了翻转链表相关的知识,希望对你有一定的参考价值。

/**
     * 翻转双向链表
     * 需要记录翻转后的下一个节点
     * @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;
    }

 

以上是关于翻转链表的主要内容,如果未能解决你的问题,请参考以下文章