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()
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)
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())
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