def copyNext(head): while head != None: newNode = RandomListNode(head.label) newNode.next = head.next newNode.random = head.random head.next = newNode head = head.next.next def copyRandom(head): while head != None: if head.random != None: head.next.random = head.random.next head = head.next.next def splitList(head): newHead = head.next while head != None: temp = head.next head.next = temp.next head = head.next if temp.next != None: temp.next = temp.next.next return newHead head = RandomListNode.instance() RandomListNode.printListbyNext(copyListwithRandomPointer(head)) RandomListNode.printListbyRandom(copyListwithRandomPointer(head))
copyRandom(head) return splitList(head) def copyNext(head): while head != None: newNode = RandomListNode(head.label) newNode.next = head.next newNode.random = head.random head.next = newNode head = head.next.next def copyRandom(head): while head != None: if head.random != None: head.next.random = head.random.next head = head.next.next def splitList(head): newHead = head.next while head != None: temp = head.next head.next = temp.next head = head.next if temp.next != None: temp.next = temp.next.next return newHead head = RandomListNode.instance() RandomListNode.printListbyNext(copyListwithRandomPointer(head)) RandomListNode.printListbyRandom(copyListwithRandomPointer(head))