LeetCode 138. Copy List with Random Pointer

本文介绍了一种特殊链表的深拷贝方法,该链表节点包含额外的随机指针。通过两种技巧——映射存储及节点复制与拆分,实现了对链表的有效复制。

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

这个可以有两种方式去做,一个是使用正常的方式,使用map进行存储,另一个是使用一个相当trick的方式进行处理,将改点复制然后放置到后面,最后拆分链表

java

/**
 * Definition for singly-linked list with a random pointer.
 * class RandomListNode {
 *     int label;
 *     RandomListNode next, random;
 *     RandomListNode(int x) { this.label = x; }
 * };
 */
public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        if (head == null) {
            return null;
        }
        head = copyNode(head);
        head = copyEdge(head);
        return splitNode(head);
    }
    
    private RandomListNode copyNode(RandomListNode head) {
        if (head == null) {
            return null;
        }
        RandomListNode node = head;
        while (node != null) {
            RandomListNode temp = new RandomListNode(node.label);
            temp.next = node.next;
            node.next = temp;
            node = node.next.next;
        }
        return head;
    }
    
    private RandomListNode copyEdge(RandomListNode head) {
        if (head == null) {
            return null;
        }
        RandomListNode node = head;
        while (node != null) {
            if (node.random != null) {
                node.next.random = node.random.next;
            }
            node = node.next.next;
        }
        return head;
    }
    
    private RandomListNode splitNode(RandomListNode head) {
        RandomListNode val = head.next;
        while (head != null) {
            RandomListNode temp = head.next;
            head.next = temp.next;
            head = head.next;
            if (temp.next != null) {
                temp.next = temp.next.next;
            }
        }
        return val;
    }
}
python

# Definition for singly-linked list with a random pointer.
# class RandomListNode(object):
#     def __init__(self, x):
#         self.label = x
#         self.next = None
#         self.random = None

class Solution(object):
    def copyRandomList(self, head):
        """
        :type head: RandomListNode
        :rtype: RandomListNode
        """
        if head is None:
            return head
        head = self.copyNode(head)
        head = self.copyEdge(head)
        return self.splitNode(head)
        
    def copyNode(self, head):
        if head is None:
            return head
        node = head
        while node is not None:
            temp =  RandomListNode(node.label)
            temp.next = node.next
            node.next = temp
            node = node.next.next
        return head
    
    def copyEdge(self, head):
        if head is None:
            return head
        node = head
        while node is not None:
            if node.random is not None:
                node.next.random = node.random.next
            node = node.next.next
        return head
        
    def splitNode(self, head):
        if head is None:
            return head
        node = head.next
        while head is not None:
            temp = head.next
            head.next = temp.next
            head = head.next
            if temp.next is not None:
                temp.next = temp.next.next
        return node
        
        
        
        
        
        
        
        
        
        
        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值