链表相关编程题总结

回眸只為那壹抹淺笑 提交于 2019-12-22 03:29:38

1、复制带随机指针的链表

class Solution(object):
    def copyRandomList(self, head):
        """
        :type head: Node
        :rtype: Node
        """
        if head is None:
            return None
        p = head
        while p:
            node = Node(x=p.val, next=p.next)
            p.next = node
            p = p.next.next
        p = head
        while p:
            if p.random:
                p.next.random = p.random.next
            else:
                p.next.random = None
            p = p.next.next
        p = head.next
        head = p
        while p.next:
            p.next = p.next.next
            p = p.next
        return head
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!