【重点】剑指offer——面试题26:复杂链表的复制

力扣

法1:哈希表,O(n)+O(n)

原Node => 新Node这种方式真是简洁大方!

Python

"""
# Definition for a Node.
class Node:
    def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
        self.val = int(x)
        self.next = next
        self.random = random
"""
class Solution:
    def copyRandomList(self, head: 'Node') -> 'Node':
        if not head:
            return None

        copy_map =  dict()
        cur = head
        while cur:
            copy_map[cur] = Node(cur.val)
            cur = cur.next
        cur = head
        while cur:
            copy_map[cur].next = None if not cur.next else copy_map[cur.next]
            copy_map[cur].random = None if not cur.random else copy_map[cur.random]
            cur = cur.next
        return copy_map[head]

Java

class Solution {
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        Node cur = head;
        Map<Node, Node> map = new HashMap();
        while (cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        while (cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }

        return map.get(head);
    }
}

法2:复制+拆分链表

需要缕清楚指向关系。

Python

class Solution:
    def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
        if head == None:
            return head

        cur = head
        while cur != None:
            next_node = cur.next
            new_node = Node(cur.val, None, None)
            cur.next = new_node
            new_node.next = next_node
            cur = next_node
        cur = head
        while cur != None:
            cur.next.random = None if cur.random == None else cur.random.next
            cur = cur.next.next
        
        cur = head.next
        while cur != None:
            next_node = cur.next
            cur.next = None if next_node == None else next_node.next
            cur = cur.next

        return head.next
        

Java

/*
// 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;
        }
        // 1.复制
        Node cur = head;
        while (cur != null) {
            Node copyCur = new Node(cur.val);
            copyCur.next = cur.next;
            cur.next = copyCur;
            cur = copyCur.next;
        }
        // 2.设置random
        cur = head;
        while (cur != null) {
            if (cur.random != null) {
                cur.next.random = cur.random.next;
            }
            cur = cur.next.next;
        }
        // 3.分割
        cur = head.next;
        Node pre = head, copyHead = head.next;
        while (cur.next != null) {
            pre.next = pre.next.next;
            cur.next = cur.next.next;
            pre = pre.next;
            cur = cur.next;
        }
        pre.next = null;

        return copyHead;
    }
}

Solution1:
小套路记住就行!!!
根据书上复杂度为 O ( n ) O(n) O(n)的算法写的,此题加深了对于链表指针的理解与应用!

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        if(pHead == NULL)
            return NULL;
        CloneNodes(pHead);
        ConnectRan(pHead);
        struct RandomListNode* pHead_clone = Get(pHead);
        return pHead_clone;
    }
    
    void CloneNodes(RandomListNode* pHead){
        struct RandomListNode* pNode = pHead;
        while(pNode != NULL){
            struct RandomListNode* pClone = new RandomListNode(pNode->label);
            pClone->next = pNode->next;
            pNode->next = pClone;
            pNode = pClone->next;
        }
        return;
    }
    
    void ConnectRan(RandomListNode* pHead){
        struct RandomListNode* pNode = pHead;
        while(pNode != NULL){
            struct RandomListNode* pClone = pNode->next;
            if(pNode->random != NULL){
                pClone->random = pNode->random->next;
            }
            pNode = pClone->next;
        }
        return;
    }
    
    struct RandomListNode* Get(RandomListNode* pHead){
        struct RandomListNode* pNode1 = pHead, *pHead_Clone = pHead->next;
        struct RandomListNode* pNode2 = pHead_Clone;
        while(pNode1 != NULL){
            pNode1->next = pNode2->next;
            pNode1 = pNode1->next;
            if(pNode1 == NULL)
                break;
            pNode2->next = pNode1->next;
            pNode2 = pNode2->next;
        }
        return pHead_Clone;
    }
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值