LC-随机链表的复制、排序链表、合并K个升序链表、LRU缓存

随机链表的复制

为了在 O(n) 时间复杂度内解决这个问题,并且使用 O(1) 的额外空间,可以利用以下技巧:

  1. 将新节点插入到原节点后面:我们可以将复制节点插入到原节点后面。例如,如果链表是 A -> B -> C,我们将链表改为 A -> A' -> B -> B' -> C -> C',其中 A'B'C'ABC 的拷贝节点。
  2. 复制 random 指针:因为复制节点与原节点紧挨在一起,我们可以直接利用原节点的 random 指针,来为新节点复制 random 指针。
  3. 拆分链表:最后,我们将原链表和复制链表拆分成两个独立的链表。
/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/

class Solution {
    public Node copyRandomList(Node head) {
        if(head == null){
            return null;
        }

        //插入新节点到原节点后面
        Node cur = head;
        while(cur != null){
            Node copy = new Node(cur.val);//创建新节点
            copy.next = cur.next;//新节点的next指向原节点的next
            cur.next = copy;//原节点的next指向新节点
            cur = copy.next;//移动到原节点的下一个节点
        }

        //复制random节点
        cur = head;
        while(cur != null){
            if(cur.random != null){
                cur.next.random = cur.random.next;//新节点的random指向原节点random对应的新节点
            }
            cur = cur.next.next;//跳到下一个原节点
        }

        //拆分链表,恢复原链表并生成新链表
        Node newHead = head.next;
        Node copyCur = newHead;
        cur = head;
        while(cur != null){
            cur.next = cur.next.next;//恢复原链表
            if(copyCur.next != null){
                copyCur.next = copyCur.next.next;//更新新链表的next指针
                copyCur = copyCur.next;
            }
            cur = cur.next;
        }
        return newHead;
    }
}

排序链表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值