LeetCode 138. Copy List with Random Pointer

来源:互联网 发布:python中的sleep 编辑:程序博客网 时间:2024/06/01 08:24

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 = Noneclass 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