leetcode 138. Copy List with Random Pointer 发表于 2018-12-28 | 分类于 leetcode 题目链接:leetcode 思路:遍历原链表,生成新链表 12345678910111213141516171819202122232425# 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 not head: return None p = head newhead = q = RandomListNode(head.label) while p: q.next = None if not p.next else RandomListNode(p.next.label) q.random = None if not p.random else RandomListNode(p.random.label) p = p.next q = q.next return newhead