# -*- coding: UTF-8 -*- # Program to find intersection of two sorted linked list import initialize def find_intersection_of_two_linked_list(head1, head2): if not head1 or not head2: raise ValueError("Linked list are empty") current1 = head1 current2 = head2 while current1 and current2: if current1.data < current2.data: current1 = current1.nextnode elif current2.data < current1.data: current2 = current2.nextnode elif current1.data == current2.data: return "Linked list intersect at %s" % str(current1.data) return 'Lists do not intersect' lList1 = initialize.initialize_linked_list_by_array([30, 15, 9, 6, 3]) lList2 = initialize.initialize_linked_list_by_array([30, 15, 10]) print find_intersection_of_two_linked_list(lList1.head, lList2.head)
total_sum += head2.data sum = carry + (total_sum % 10) carry = total_sum / 10 temp = linked_list.Node(sum) if curr: curr.nextnode = temp curr = curr.nextnode else: ll = linked_list.LinkedList(temp) curr = ll.head if head1: head1 = head1.nextnode if head2: head2 = head2.nextnode if carry: curr.nextnode = linked_list.Node(carry) return ll lList1 = initialize.initialize_linked_list_by_array([8, 0, 0]) lList2 = initialize.initialize_linked_list_by_array([8, 0, 0]) ll = add(lList1.head, lList2.head) ll.print_list()
if not head1 or not head2: raise ValueError ("Linked list are empty") current1 = head1 current2 = head2 while current1 and current2: if current1.data < current2.data: current1 = current1.nextnode elif current2.data < current1.data: current2 = current2.nextnode elif current1.data == current2.data: temp = Node(current1.data) if head == None: head = temp tail = head else: tail.nextnode = temp tail = tail.nextnode current1 = current1.nextnode current2 = current2.nextnode return head lList1 = initialize.initialize_linked_list_by_array([6, 4, 3, 2, 1]) lList2 = initialize.initialize_linked_list_by_array([8, 6, 4, 2]) head = find_intersection_of_two_linked_list(lList1.head, lList2.head, None) while (head): print head.data head = head.nextnode
sum = carry + (total_sum % 10) carry = total_sum / 10 temp = linked_list.Node(sum) if curr: curr.nextnode = temp curr = curr.nextnode else: ll = linked_list.LinkedList(temp) curr = ll.head if head1: head1 = head1.nextnode if head2: head2 = head2.nextnode if carry: curr.nextnode = linked_list.Node(carry) return ll lList1 = initialize.initialize_linked_list_by_array([8, 0, 0]) lList2 = initialize.initialize_linked_list_by_array([8, 0, 0]) ll = add(lList1.head, lList2.head) ll.print_list()
temp1 = head1 temp2 = head2 prev1 = None while (temp1 and temp2): prev1 = temp1 next1 = temp1.nextnode prev2 = temp2 next2 = temp2.nextnode prev1.nextnode = prev2 prev2.nextnode = next1 temp2 = next2 temp1 = next1 if temp2: prev2.nextnode = temp2 return head1 lList1 = initialize.initialize_linked_list() lList2 = initialize.initialize_linked_list_by_array([4, 5, 6, 7, 8, 9]) head1 = alternate_merge(lList1.head, lList2.head) while (head1): print head1.data head1 = head1.nextnode
# -*- coding: UTF-8 -*- # Program to find intersection of two sorted linked list import initialize def find_intersection_of_two_linked_list(head1, head2): if not head1 or not head2: raise ValueError ("Linked list are empty") current1 = head1 current2 = head2 while current1 and current2: if current1.data < current2.data: current1 = current1.nextnode elif current2.data < current1.data: current2 = current2.nextnode elif current1.data == current2.data: return "Linked list intersect at %s"%str(current1.data) return 'Lists do not intersect' lList1 = initialize.initialize_linked_list_by_array([30,15,9,6, 3]) lList2 = initialize.initialize_linked_list_by_array([30, 15, 10]) print find_intersection_of_two_linked_list(lList1.head, lList2.head)