def getReversedList(head_in): list_new = LinkedList() curr = head_in while curr: list_new.insert(curr.val, 0) curr = curr.next return list_new.getHeadNode()
## Tests For Singly Linked List Implementation from myllist import LinkedList list1 = LinkedList() print "Size is: " + str(list1.size()) list1.append(1) list1.append(2) list1.append(3) print "Size is: " + str(list1.size()) for idx in range(list1.size()): print list1.get(idx) list1.insert(4, 3) print "Size is: " + str(list1.size()) for idx in range(list1.size()): print list1.get(idx) list1.remove(3) print "Size is: " + str(list1.size()) for idx in range(list1.size()): print list1.get(idx)
## If we had a doubly linked list (with a tail pointer), we could have done this in O(n/2) import sys sys.path.append('./classes') from myllist import LinkedList def getReversedList(head_in): list_new = LinkedList() curr = head_in while curr: list_new.insert(curr.val, 0) curr = curr.next return list_new.getHeadNode() def isPalindrome(head1): head2 = getReversedList(head1) while head1: if head1.val != head2.val: return False head1 = head1.next head2 = head2.next return True list1 = LinkedList() for n in "": list1.append(n) print isPalindrome(list1.getHeadNode())
return head1.val * multiplier + head2.val * multiplier + sumForwardHelper( head1.next, head2.next, multiplier / 10) def findMaxMultiplier(head1, head2): length1 = 0 curr = head1 while curr: length1 += 1 curr = curr.next length2 = 0 curr = head2 while curr: length2 += 1 curr = curr.next return pow(10, max(length1, length2) - 1) list1 = LinkedList() for n in [7, 1, 6]: list1.append(n) list2 = LinkedList() for n in [5, 9, 2]: list2.append(n) headNode1 = list1.getHeadNode() headNode2 = list2.getHeadNode() print sumReverse(headNode1, headNode2) print sumForward(headNode1, headNode2)
## CTCI - Linked Lists Question 3 ## Delete middle element, given only access to that element import sys sys.path.append('./classes') from myllist import LinkedList def deleteMiddleElmt(mnode): if mnode is None or mnode.next is None: return curr = mnode nextElmt = mnode.next while nextElmt.next: tmp = curr.next curr.val = nextElmt.val curr = tmp nextElmt = tmp.next curr.val = nextElmt.val curr.next = None list1 = LinkedList() for n in [1, 2, 3, 4, 5]: list1.append(n) middleNode = list1.getNode(3) list1.printList() deleteMiddleElmt(middleNode) list1.printList()
def returnLoopNode(head): if head is None: return None itemSet = Set() runner = head itemSet.add(head) while runner.next: if runner.next in itemSet: return runner.next itemSet.add(runner.next) runner = runner.next return None list1 = LinkedList() for n in [1, 2, 3, 4, 5, 6]: list1.append(n) endNode = list1.getNode(5) loopNode = list1.getNode(2) endNode.next = loopNode headNode = list1.getHeadNode() result = returnLoopNode(headNode) if result: print result.val else: print result
def partition(head,x): if head is None: return searcher = head partitioner = head while partitioner and partitioner.val <= x: partitioner = partitioner.next searcher = searcher.next while searcher: if searcher.val > x: searcher = searcher.next else: smallVal = searcher.val searcher.val = partitioner.val partitioner.val = smallVal partitioner = partitioner.next searcher = searcher.next list1 = LinkedList() for n in [6,2,3,4,5,7]: list1.append(n) headNode = list1.getHeadNode() list1.printList() partition(headNode,3) list1.printList()
## CTCI - Linked Lists Question 2 ## Return kth last element of the list (k=0 returns last element) ## Assumes we know the size of the list ## Assumes we want the value of the element, not the node itself import sys sys.path.append('./classes') from myllist import LinkedList def returnKthToLast(inlist, k): totalSize = inlist.size() if totalSize > 0: stop = totalSize - 1 - k return returnKthToLastHelper(inlist.getHeadNode(), stop, 0) return None def returnKthToLastHelper(curr, stop, pos): if pos == stop: return curr.val return returnKthToLastHelper(curr.next, stop, pos + 1) list1 = LinkedList() for n in [1, 2, 3, 4, 5, 6]: list1.append(n) for n in [0, 1, 2, 3, 4, 5]: print returnKthToLast(list1, n)