def getLeng(node): count = 0 while node != None: count += 1 node = node.next return count def addNumbers(h1, h2): l1, l2 = getLeng(h1), getLeng(h2) needToAdd1 = addNumRecur(h1, h2, l1 >= l2, abs(l1 - l2)) if needToAdd1: newN = ListNode(1) newN.next = (h1 if l1 >= l2 else h2) return newN else: return (h1 if l1 >= l2 else h2) n1, n2, n3 = ListNode(5), ListNode(6), ListNode(3) n1.next = n2 n2.next = n3 N1, N2, N3, N4 = ListNode(8), ListNode(4), ListNode(2), ListNode(2) N1.next = N2 N2.next = N3 # N3.next = N4 pn.printNode(addNumbers(n1, N1))
import PrintNode as pn from ListNode import ListNode def mergeLinkedList(h1, h2): p1, p2 = h1, h2 while p1 != None and p2 != None: tmp = p1.next p1.next = p2 p2 = p2.next p1.next.next = tmp p1 = tmp return h1, p2 n1, n2, n3 = ListNode(1), ListNode(2), ListNode(3) n1.next = n2 n2.next = n3 N1, N2, N3, N4, N5 = ListNode(4), ListNode(5), ListNode(6), ListNode( 7), ListNode(8) N1.next = N2 N2.next = N3 # N3.next = N4 # N4.next = N5 r1, r2 = mergeLinkedList(n1, N1) pn.printNode(r1) print "--------------------" pn.printNode(r2)
def mergeSort(head): if head == None or head.next == None: return head fast, slow = head, head while fast.next != None and fast.next.next != None: fast = fast.next.next slow = slow.next l, r, slow.next = head, slow.next, None l = mergeSort(l) r = mergeSort(r) return merge(l, r) N1, N2, N3, N4, N5, N6, N7, N8, N9, N10 = ListNode(11), ListNode(2), ListNode( 31), ListNode(14), ListNode(25), ListNode(64), ListNode(7), ListNode( 18), ListNode(49), ListNode(15) N1.next = N2 N2.next = N3 N3.next = N4 N4.next = N5 N5.next = N6 N6.next = N7 N7.next = N8 N8.next = N9 N9.next = N10 pn.printNode(N1) print '----------------------' pn.printNode(mergeSort(N1))
return head.next def mergeSort(head): if head == None or head.next == None: return head fast, slow = head, head while fast.next != None and fast.next.next != None: fast = fast.next.next slow = slow.next l, r, slow.next = head, slow.next, None l = mergeSort(l) r = mergeSort(r) return merge(l, r) N1, N2, N3, N4, N5, N6, N7, N8, N9, N10 = ListNode(11), ListNode(2), ListNode(31), ListNode(14), ListNode(25), ListNode(64), ListNode(7), ListNode(18), ListNode(49), ListNode(15) N1.next = N2 N2.next = N3 N3.next = N4 N4.next = N5 N5.next = N6 N6.next = N7 N7.next = N8 N8.next = N9 N9.next = N10 pn.printNode(N1) print '----------------------' pn.printNode(mergeSort(N1))
def getLeng(node): count = 0 while node != None: count += 1 node = node.next return count def addNumbers(h1, h2): l1, l2 = getLeng(h1), getLeng(h2) needToAdd1 = addNumRecur(h1, h2, l1 >= l2, abs(l1-l2)) if needToAdd1: newN = ListNode(1) newN.next = (h1 if l1>=l2 else h2) return newN else: return (h1 if l1>=l2 else h2) n1, n2, n3 = ListNode(5), ListNode(6), ListNode(3) n1.next = n2 n2.next = n3 N1, N2, N3, N4 = ListNode(8), ListNode(4), ListNode(2), ListNode(2) N1.next = N2 N2.next = N3 # N3.next = N4 pn.printNode(addNumbers(n1, N1))
return h_interset.next, h_union.next n1, n2, n3, n4, n5, n6 = ListNode(2), ListNode(2), ListNode(3), ListNode( 4), ListNode(5), ListNode(10) n1.next = n2 n2.next = n3 n3.next = n4 n4.next = n5 n5.next = n6 N1, N2, N3, N4, N5, N6, N7, N8 = ListNode(10), ListNode(2), ListNode( 31), ListNode(14), ListNode(21), ListNode(53), ListNode(11), ListNode(13) N1.next = N2 N2.next = N3 N3.next = N4 N4.next = N5 N5.next = N6 N6.next = N7 N7.next = N8 pn.printNode(n1) print '----------------------' pn.printNode(N1) print '----------------------' interset, union = unionAndIntersection(n1, N1) pn.printNode(interset) print '----------------------' pn.printNode(union)
import PrintNode as pn from ListNode import ListNode def deleteNode(head, node): if head == None: return if node.val == head.val: return head.next fast, slow = head.next, head while fast != None and fast.next != None: if fast.val == node.val: slow.next = fast.next return head else: slow = fast fast = fast.next if fast.val == node.val: slow.next = None return head n1, n2, n3, n4, n5 = ListNode(1), ListNode(2), ListNode(5), ListNode(10), ListNode(11) node = ListNode(100) n1.next = n2 n2.next = n3 n3.next = n4 n4.next = n5 pn.printNode(deleteNode(n1, node))
else: return (head, head) def reverseList(head, k): head_lastPart, count = head, 1 while head_lastPart != None and count <= k: head_lastPart = head_lastPart.next count += 1 if head_lastPart == None and count < k: return #k longer than the list. simply not possible newTail, newHead = reverseListRecur(head, k) newTail.next = head_lastPart return newHead N1, N2, N3, N4, N5, N6, N7, N8 = ListNode(1), ListNode(2), ListNode( 3), ListNode(4), ListNode(5), ListNode(6), ListNode(7), ListNode(8) N1.next = N2 N2.next = N3 N3.next = N4 N4.next = N5 N5.next = N6 N6.next = N7 N7.next = N8 pn.printNode(reverseList(N1, 8))
newTail, newHead = reverseListRecur(head.next, k-1) newTail.next = head return (head, newHead) else: return (head, head) def reverseList(head, k): head_lastPart, count = head, 1 while head_lastPart != None and count <= k: head_lastPart = head_lastPart.next count += 1 if head_lastPart == None and count < k: return #k longer than the list. simply not possible newTail, newHead = reverseListRecur(head, k) newTail.next = head_lastPart return newHead N1, N2, N3, N4, N5, N6, N7, N8 = ListNode(1), ListNode(2), ListNode(3), ListNode(4), ListNode(5), ListNode(6), ListNode(7), ListNode(8) N1.next = N2 N2.next = N3 N3.next = N4 N4.next = N5 N5.next = N6 N6.next = N7 N7.next = N8 pn.printNode(reverseList(N1, 8))
count, p1 = 0, head while count != numNodes: p1 = p1.next count += 1 p1_slow, p2 = p1, head while p2 != p1: p1_slow = p1 p1 = p1.next p2 = p2.next p1_slow.next = None return head N1, N2, N3, N4, N5, N6, N7, N8, N9, N10 = ListNode(1), ListNode(2), ListNode(3), ListNode(4), ListNode(5), ListNode(6), ListNode(7), ListNode(8), ListNode(9), ListNode(10) N1.next = N2 N2.next = N3 N3.next = N4 N4.next = N5 N5.next = N6 N6.next = N7 N7.next = N8 N8.next = N9 N9.next = N10 N10.next = N4 pn.printNode(detectAndRemoveLoop(N1))