def loop_detection(ll): fast = slow = ll.head while fast and fast.next: print(slow.value, fast.value) fast = fast.next.next slow = slow.next if fast == slow: break print(slow.value, fast.value) if fast is None or fast.next is None: return None print('---') slow = ll.head while fast != slow: print(slow.value, fast.value) fast = fast.next slow = slow.next print(slow.value, fast.value) print(fast.value) ll = LinkedList() ll.append_multiple([1, 2, 3, 4]) node = Node(5) ll.append_multiple([node, Node(6), Node(7), Node(8), Node(9), node]) loop_detection(ll)
diff = longer.length - shorter.length short, long = shorter.head, longer.head for i in range(diff): long = long.next while short is not long: short = short.next long = long.next return long.value ll_1 = LinkedList() ll_2 = LinkedList() ll_1.append_multiple([2, 8, 4, 9, 2]) ll_2.append_multiple([3, 4, 1]) node = Node(5) ll_1.append(node) ll_2.append(node) node = Node(1) ll_1.append(node) ll_2.append(node) ll_1.display() ll_2.display() result = intersection(ll_1, ll_2) print(result)
def sum_lists(ll_a, ll_b): n1, n2 = ll_a.head, ll_b.head ll = LinkedList() carry = 0 while n1 or n2: result = carry if n1: result += n1.value n1 = n1.next if n2: result += n2.value n2 = n2.next ll.append(result % 10) carry = result // 10 if carry: ll.append(carry) return ll ll_a = LinkedList() ll_a.append_multiple([6, 1, 7]) ll_b = LinkedList() ll_b.append_multiple([2, 9, 5]) ll_a.display() ll_b.display() ll_sum = sum_lists(ll_a, ll_b) ll_sum.display()
from utils import LinkedList def delete_middle_node(node): node.value = node.next.value node.next = node.next.next ll = LinkedList() ll.append_multiple([1, 2, 3, 4]) middle_node = ll.append(5) ll.append_multiple([6, 7, 8, 9]) ll.display() delete_middle_node(middle_node) ll.display()
from utils import LinkedList def palindrome_runner(ll): runner = current = ll.head stack = [] while runner and runner.next: stack.append(current.value) current = current.next runner = runner.next.next if runner: current = current.next while current: top = stack.pop() if top != current.value: return False current = current.next return True ll = LinkedList() ll.append_multiple([1, 2, 3, 4, 3, 2, 1]) ll.display() result = palindrome_runner(ll) print(result)
from utils import LinkedList def partition(ll, x): current = ll.tail = ll.head while current: nextNode = current.next current.next = None if current.value < x: current.next = ll.head ll.head = current else: ll.tail.next = current ll.tail = current current = nextNode # Error check in case all nodes are less than x if ll.tail.next is not None: ll.tail.next = None return ll ll = LinkedList() n = ll.append(55) ll.append_multiple([82, 95, 20, 47, 37, 11, 42, 91, 6, 74]) ll.display() print('Patition created at {}'.format(n.value)) ll = partition(ll, n.value) ll.display()
from utils import LinkedList def kth_to_last(ll, k): if k <= 0: raise (AttributeError('Set k to a value greater than 0')) current = runner = ll.head for i in range(k): runner = runner.next while runner: current = current.next runner = runner.next return current.value ll = LinkedList() ll.append_multiple([1, 2, 3, 4, 5, 6, 7, 8, 9]) kth_to_last = kth_to_last(ll, 2) print(kth_to_last)