def copy_linked_list(ll): ll_copy = LinkedList(Node(None)) for node in ll: ll_copy.current.value = node.value if node.next is not None: ll_copy.current.next = Node(None) ll_copy.current = ll_copy.current.next return ll_copy
def P2_6(): # 2.6 Palindrome ll = LinkedList(Node('a')) second = Node('b') ll.head.next = second third = Node('c') second.next = third print(ll) print(is_palindrome(ll))
def P2_8(): # 2.8 Loop Detection ll = LinkedList(Node(1)) second = Node(2) ll.head.next = second third = Node(3) second.next = third fourth = Node(4) third.next = fourth fifth = second fourth.next = fifth out = linked_list_loop_detection(ll) if out is not None: out = out.value print("First Node In Loop: ", out)
def sum_lists(ll_a, ll_b): # write a function that adds up the values in two linked lists carry = 0 ll_a.current = ll_a.head ll_b.current = ll_b.head ll_sum = LinkedList(Node(None)) while not (ll_a.current is None and ll_b.current is None): if ll_a.current is not None: term_a = ll_a.current.value ll_a.current = ll_a.current.next else: term_a = 0 if ll_b.current is not None: term_b = ll_b.current.value ll_b.current = ll_b.current.next else: term_b = 0 digit = (term_a + term_b) % 10 ll_sum.current.value = digit + carry if 10 <= (term_a + term_b): carry = 1 else: carry = 0 if (carry is 1) or (not (ll_a.current is None and ll_b.current is None)): ll_sum.current.next = Node(None) ll_sum.current = ll_sum.current.next ll_a.current = ll_a.head ll_b.current = ll_a.head ll_sum.current = ll_sum.head return ll_sum
def reverse_linked_list(ll, in_place=True): # write a function that reverses a linked list prev = None tmp = LinkedList(Node(None)) if in_place is True: for node in ll: if node.next is None: ll.head = node node.next = prev prev = node return ll else: for node in ll: if node.next is None: tmp.head = tmp.current tmp.current.next = prev prev = tmp.current
def P2_7(): # 2.7 Intersection ll_a = LinkedList(Node(1)) a_second = Node(2) ll_a.head.next = a_second a_third = Node(3) a_second.next = a_third ll_b = LinkedList(Node(3)) b_second = a_second ll_b.head.next = b_second b_third = Node(1) b_second = b_third print("Linked List A: ", ll_a) print("Linked List B: ", ll_b) print("Intersection: ", linked_list_intersection(ll_a, ll_b).value)
def P2_5(): #2.5 SumLists ll_a = LinkedList(Node(2)) a_second = Node(1) ll_a.head.next = a_second a_third = Node(5) a_second.next = a_third ll_b = LinkedList(Node(1)) b_second = Node(2) ll_b.head.next = b_second print("Linked List A: ", ll_a) reverse_linked_list(ll_a) print("Reversed Linked List A: ", ll_a) print("Linked List B: ", ll_b) reverse_linked_list(ll_b) print("Reversed Linked List B: ", ll_b) ll_sum = sum_lists(ll_a, ll_b) print("Sum of Lists: ", ll_sum) reverse_linked_list(ll_sum) print("Reversed Sum of Lists: ", ll_sum)
def P2_4(): #2.4 Partition ll = LinkedList(Node(10)) second = Node(55) ll.head.next = second third = Node(10) second.next = third fourth = Node(11) third.next = fourth fifth = Node(1) fourth.next = fifth partition(11, ll) print(ll)
def P2_3(): #2.3 Delete Middle Node ll = LinkedList(Node(10)) second = Node(55) ll.head.next = second third = Node(10) second.next = third fourth = Node(11) third.next = fourth fifth = Node(1) fourth.next = fifth delete_middle_node(second) print(ll)
def P2_2(): #2.2 Kth to Last ll = LinkedList(Node(10)) second = Node(55) ll.head.next = second third = Node(10) second.next = third fourth = Node(11) third.next = fourth fifth = Node(1) fourth.next = fifth print(kth_to_last(int(input("pick your k ")), ll))
def P2_1(): # 2.1 Remove Dupes ll = LinkedList(Node(10)) second = Node(55) ll.head.next = second third = Node(10) second.next = third fourth = Node(11) third.next = fourth fifth = Node(1) fourth.next = fifth print('===Linked List Representation===') print(ll) print('================================') print('===FOR LOOP===') for node in ll: print(node.value) print('==============') print('===NO DUPES===') remove_dupes(ll) print(ll) print('==============')