def add2numbers(head1, head2): count1 = 0 temp = head1 while temp: count1 += 1 temp = temp.next count2 = 0 temp = head2 while temp: count2 += 1 temp = temp.next carry = 0 if count1 == count2: carry = add_same_size_list(head1, head2) else: if count2 < count1: while count2 - count1 != 0: new_node = BasicOperations.Node(0) new_node.next = head2 head2 = new_node count2 += 1 else: while count1 - count2 != 0: new_node = BasicOperations.Node(0) new_node.next = head1 head1 = new_node count1 += 1 carry = add_same_size_list(head1, head2) if carry != 0: global head new_node = BasicOperations.Node(carry) new_node.next = head head = new_node
def merge_two_sorted_ll(list1, list2): if list1.head is None: return list2 if list2.head is None: return list1 merged_list = BasicOperations.LinkedList() l1 = list1.head l2 = list2.head if l1.data < l2.data: merged_list.head = list1.head l1 = l1.next else: merged_list.head = list2.head l2 = l2.next temp = merged_list.head while l1 is not None and l2 is not None: if l1.data > l2.data: temp.next = l2 l2 = l2.next temp = temp.next else: temp.next = l1 l1 = l1.next temp = temp.next if l1 is not None: temp.next = l1 if l2 is not None: temp.next = l2 return merged_list
def insert(data): global head new_node = BasicOperations.Node(data) if head is None: head = new_node else: new_node.next = head head = new_node
def nth_from_the_end(list1, n): fast = list1.head slow = list1.head for i in range(n - 1): fast = fast.next while fast.next: fast = fast.next slow = slow.next return slow def middle_node(list1): fast = list1.head slow = list1.head while fast and fast.next: fast = fast.next.next slow = slow.next return slow if __name__ == '__main__': list1 = BasicOperations.LinkedList() list1.insert_values([1, 2, 3, 4, 5, 6, 7]) nth_node = nth_from_the_end(list1, 3) print("3rd node from the list") print(nth_node.data) print("Middle of the list") middle = middle_node(list1) print(middle.data)
return False def cycle(list1): # Another method to find cycle temp = list1.head if list1.head is None: return False if list1.head.next is None: return False while temp: temp.data = None temp = temp.next if temp.data is None: return True return False if __name__ == '__main__': list1 = BasicOperations.LinkedList() new_node = BasicOperations.Node(3) list1.head = new_node new_node = BasicOperations.Node(4) list1.head.next = new_node new_node = BasicOperations.Node(5) list1.head.next.next = new_node new_node = BasicOperations.Node(6) list1.head.next.next.next = new_node new_node = BasicOperations.Node(7) list1.head.next.next.next.next = list1.head.next print(cycle(list1))
else: if count2 < count1: while count2 - count1 != 0: new_node = BasicOperations.Node(0) new_node.next = head2 head2 = new_node count2 += 1 else: while count1 - count2 != 0: new_node = BasicOperations.Node(0) new_node.next = head1 head1 = new_node count1 += 1 carry = add_same_size_list(head1, head2) if carry != 0: global head new_node = BasicOperations.Node(carry) new_node.next = head head = new_node if __name__ == '__main__': list1 = BasicOperations.LinkedList() list1.insert_values([7, 2, 4, 3]) list2 = BasicOperations.LinkedList() list2.insert_values([5, 6, 4]) add2numbers(list1.head, list2.head) list3 = BasicOperations.LinkedList() list3.head = head list3.display()
prev = None temp = list1.head forward = temp.next while temp: temp.next = prev prev = temp temp = forward if forward: forward = forward.next list1.head = prev def reverse_recursive(node, prev=None): if node is None: return prev forward = node.next node.next = prev return reverse_recursive(forward, node) if __name__ == '__main__': link_list = BasicOperations.LinkedList() link_list.insert_values([4, 3, 2, 0, 6, 8, 7]) print("Before Reversing:") link_list.display() reverse_iterative(link_list) print("After Reversing") link_list.display() print("After Reversing Recursively") link_list.head = reverse_recursive(link_list.head) link_list.display()
difference = abs(counta - countb) tempa = list1.head tempb = list2.head for _ in range(difference): if counta > countb: tempa = tempa.next else: tempb = tempb.next while tempa is not None and tempb is not None: if tempa == tempb: return tempa tempa = tempa.next tempb = tempb.next return None if __name__ == '__main__': list1 = BasicOperations.LinkedList() list1.insert_end(3) list1.head.next = BasicOperations.Node(4) list1.head.next.next = BasicOperations.Node(5) list1.head.next.next.next = BasicOperations.Node(7) list2 = BasicOperations.LinkedList() list2.insert_end(1) list2.head.next = list1.head.next intersection_point = intersect(list1, list2) if intersection_point: print(f"Intersection point:{intersection_point.data}") else: print("No intersection point")