第八题:翻转双向链表

版权声明:Please make the source marked https://blog.csdn.net/qq_31807385/article/details/86187383

题目:

翻转双向链表,时间复杂度为O(N),额外空间复杂度我O(1)。

代码实现与分析:

package com.isea.brush;

/**
 * 反转双向链表
 */
public class ReverseDoubleList {
    private static class DNode {
        private int data;
        private DNode last;
        private DNode next;

        public DNode(int data) {
            this.data = data;
        }
    }

    /**
     * 辅助节点 cur 和 pre,
     * cur : 当前的节点
     * pre : 当前节点的前一个节点
     * @param head
     */
    public static void reverseDouble(DNode head) {
        DNode pre = null;
        DNode cur = null;
        while(head != null){
            cur = head.next;
            head.next = pre;
            head.last = cur;
            pre = head;
            head = cur;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_31807385/article/details/86187383
今日推荐