def test_prepare(): list1 = list_util.LList(name="List1") for i in xrange(6): list1.append(i) list2 = list_util.LList(name="List2") list2.append(100) return (list1, list2)
def test_2(): print "####### test_2 starts ########" test_list = list_util.LList() for i in xrange(5): test_list.append(i) print "Input: " test_list.print_list() result = reverse_list_recursive(test_list.head) dummy_list = list_util.LList(result) print "Output: " dummy_list.print_list() print "####### test_1 ends ########"
def test_1(): print "####### test_1 starts ########" test_list = list_util.LList() for i in xrange(5): test_list.append(i) print "Input: " test_list.print_list() result = reverse_list_iterative(test_list) dummy_list = list_util.LList(result) print "Output: " dummy_list.print_list() print "####### test_1 ends ########"
def test_1(): print "###### test_1 starts ######" list1 = list_util.LList() for i in xrange(1, 7, 2): list1.append(i) list2 = list_util.LList() for i in xrange(2, 8, 2): list2.append(i) print "Input: " list1.print_list() list2.print_list() result = merge_sorted_list(list1, list2) print "Output: " list_result = list_util.LList(result) list_result.print_list() print "###### test_1 ends ######"
def is_list_palindrome(head): """ Input: list Output: True is list is palindrome otherwise False Description: Brute force is to compare first and last, second and seconds last node but that is O(n^2). To do this efficiently, we can compare first half with reverse of second half. If they are equal then return True otherwise False. This is now O(n)""" # Skip first half slow = fast = head while fast != None and fast.next != None: fast = fast.next.next slow = slow.next if slow == None: return False second_half_list = list_util.LList(slow) first_half = head second_half = reverse_list.reverse_list_iterative(second_half_list) while second_half != None and first_half != None: if first_half.data != second_half.data: return False first_half = first_half.next second_half = second_half.next return True
def test_2(): # Add number to list in reverse order print "###### test_2 starts ######" list1 = list_util.LList(name="Num1") list1.append(0) list1.append(0) list1.append(1) list1.print_list() list2 = list_util.LList(name="Num2") list2.append(0) list2.append(0) list2.append(9) list2.print_list() sum_list = add_large_nos(list1.head, list2.head) sum_list.print_list() print "###### test_2 ends ######"
def test_prepare(): list1 = list_util.LList() list1.append(1) list1.append(2) list1.append(3) list1.append(4) list1.print_list() return list1
def is_overlapping_list(head1, head2): """ Input: list1 and list2 Output: Overlapping node or None Description: If 2 list overlap then they will have common tail node. Let x be length of list1 and y be length of list2. x-y gives you the difference by which both list are apart. Move longer list by this difference and then move both list together. The point where they meet is the overlapping node. """ if head1 == None: print "head1 is None" return head2 if head2 == None: print "head2 is None" return head dummy_list = list_util.LList(head1) len_A = dummy_list.count() dummy_list = list_util.LList(head2) len_B = dummy_list.count() print "len_A: %d len_B: %d" % (len_A, len_B) if len_A >= len_B: long_list = head1 short_list = head2 else: long_list = head2 short_list = head1 print "long_list: %d short_list: %d" % (long_list.data, short_list.data) diff_len = abs(len_A - len_B) while diff_len > 0: long_list = long_list.next diff_len -= 1 print "long_list moved to %d" % (long_list.data) while long_list != None and short_list != None and \ long_list != short_list: long_list = long_list.next short_list = short_list.next if long_list == short_list: return long_list return None
def test_3(): print "###### test_3 starts ######" list1 = list_util.LList() list1.append(1) list1.append(2) list1.append(0) list1.append(1) list1.print_list() result = is_list_palindrome(list1.head) if result == False: print "Output: False" else: print "Error: expected False" print "###### test_3 ends ######"
def add_large_nos(num1, num2): sum_list = list_util.LList(name="Sum") carry = 0 while num1 != None or num2 != None: local_sum = carry if num1 != None: local_sum += num1.data num1 = num1.next if num2 != None: local_sum += num2.data num2 = num2.next sum_list.append(local_sum % 10) # sum_list.print_list() carry = local_sum / 10 # print "local_sum %d carry %d" % (local_sum, carry) if carry != 0: sum_list.append(carry) return sum_list