复杂链表的复制

作者: 而立之年的技术控 | 来源:发表于2019-12-21 18:12 被阅读0次
    微信图片_20191221181154.jpg
    class Solution:
        # 返回 RandomListNode
        def Clone(self, pHead):
            # write code here
            if not pHead:
                return None
            tmp = pHead
            while tmp:
                node = RandomListNode(tmp.label)
                node.next = tmp.next
                tmp.next = node
                tmp = tmp.next.next
            tmp = pHead
            while tmp and tmp.next:
                if tmp.random:
                    tmp.next.random = tmp.random.next
                tmp = tmp.next.next
            tmp = pHead
            newHead = tmp.next
            newTmp = newHead
            while tmp and tmp.next:
                tmp.next = tmp.next.next
                if newTmp.next:
                    newTmp.next = newTmp.next.next
                    newTmp = newTmp.next
                tmp = tmp.next
            return newHead
    

    相关文章

      网友评论

        本文标题:复杂链表的复制

        本文链接:https://www.haomeiwen.com/subject/noignctx.html