链表相关编程题总结

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
posted @ 2019-12-22 03:11  WESWES  阅读(332)  评论(0编辑  收藏  举报