def sum_reverse(lst1, lst2): sum_lst = [] step = 0 h1 = lst1.head h2 = lst2.head while h1 != None or h2 != None: n1 = h1.data if h1 else 0 n2 = h2.data if h2 else 0 acc = n1 + n2 + step if acc >= 10: step = 1 acc -= 10 else: step = 0 sum_lst.append(acc) if h1: h1 = h1.next if h2: h2 = h2.next if step > 0: sum_lst.append(step) return LinkedList(sum_lst)
class AnimalShelter: def __init__(self): self.cats = LinkedList([]) self.dogs = LinkedList([]) self.__stamp = 0 def enqueue(self, animal: Animal): animal.timestamp = self.__stamp self.__stamp += 1 if isinstance(animal, Cat): self.cats.append(animal) else: self.dogs.append(animal) def dequeue_any(self): if self.cats.head == None: return self.dequeue_dog() elif self.dogs.head == None: return self.dequeue_cat() dog = self.dogs.head.data cat = self.cats.head.data if (dog > cat): return self.dequeue_dog() else: return self.dequeue_cat() def dequeue_dog(self): if self.dogs.head == None: raise Exception() tmp = self.dogs.head.data self.dogs.head = self.dogs.head.next return tmp def dequeue_cat(self): if self.cats.head == None: raise Exception() tmp = self.cats.head.data self.cats.head = self.cats.head.next return tmp
class SimpleAnimalShelter: CAT = 1 DOG = 2 def __init__(self): self.list = LinkedList([]) def enqueue(self, animal): self.list.append(animal) def dequeue(self): oldest = self.list.head if oldest == None: raise Exception() self.list.head = self.list.head.next return oldest def dequeue_cat(self): return self.__dequeue_animal(SimpleAnimalShelter.CAT) def dequeue_dog(self): return self.__dequeue_animal(SimpleAnimalShelter.DOG) def __dequeue_animal(self, animal): if self.list.head == None: raise Exception() elif self.list.head.data == animal: tmp = self.list.head self.list.head = self.list.head.next return tmp oldest = None n = self.list.head while n.next != None and n.next.data != animal: n = n.next if n.next == None: raise Exception() oldest = n.next n.next = n.next.next return oldest
def partition(lst, pivot): hl = LinkedList([0]) pl = hl.head hr = LinkedList([0]) pr = hr.head runner = lst.head while runner != None: if runner.data < pivot: pl.next = runner pl = pl.next else: pr.next = runner pr = pr.next runner = runner.next pr.next = None if hl.head.next: pl.next = hr.head.next lst.head = hl.head.next else: lst.head = hr.head.next
def gen_depth_list(root): if not root: return [] q: queue.Queue = queue.Queue() q.put(root) lst = [] current = LinkedList([root]) while len(current) > 0: lst.append(current) parents = current current = LinkedList([]) for parent in parents: if parent.left: current.append(parent.left) if parent.right: current.append(parent.right) return lst
def insert_before(head, val) -> LinkedList.Node: node = LinkedList.Node(val) node.next = head return node
def sum_list(lst1: LinkedList, lst2: LinkedList) -> LinkedList: if (n1 := lst1.len()) > (n2 := lst2.len()):
else: step = 0 sum_lst.append(acc) if h1: h1 = h1.next if h2: h2 = h2.next if step > 0: sum_lst.append(step) return LinkedList(sum_lst) print(sum_reverse(LinkedList([4, 3, 2, 1]), LinkedList([8, 9, 9]))) print(sum_reverse(LinkedList([1, 1, 1]), LinkedList([9, 9, 9]))) def sum_list(lst1: LinkedList, lst2: LinkedList) -> LinkedList: if (n1 := lst1.len()) > (n2 := lst2.len()): lst2.head = padding(lst2.head, n1 - n2) else: lst1.head = padding(lst1.head, n2 - n1) (digits, carry) = sum_helper(lst1.head, lst2.head) lst = LinkedList([0]) if carry == 0: lst.head = digits else: lst.head = insert_before(digits, carry)
else: lst.head = hr.head.next def partition_opt(lst, pivot): pl = lst.head pr = lst.head runner = lst.head while runner != None: nxt = runner.next if runner.data < pivot: # insert node at head runner.next = pl pl = runner else: pr.next = runner pr = runner runner = nxt pr.next = None lst.head = pl a = LinkedList([7, 3, 5, 8, 5, 10, 2]) partition(a, 5) print(a) b = LinkedList([7, 3, 5, 8, 5, 10, 2]) partition_opt(b, 5) print(b)
# time: O(N^2) space: O(1) def remove_dup2(a: LinkedList): flag: LinkedList.Node = a.head if flag == None: return while flag != None: runner = flag while runner.next != None: if runner.next.data == flag.data: runner.next = runner.next.next else: runner = runner.next flag = flag.next a = LinkedList([1, 2, 2, 1]) remove_dup(a) print(a) b = LinkedList([7, 5, 5, 3, 1, 5, 1]) remove_dup(b) print(b) c = LinkedList([1, 2, 2, 1]) d = LinkedList([7, 5, 5, 3, 1, 5, 1]) remove_dup2(c) remove_dup2(d) print(c) print(d)
def __init__(self): self.cats = LinkedList([]) self.dogs = LinkedList([]) self.__stamp = 0
def __init__(self): self.list = LinkedList([])
def delete_node(n: LinkedList.Node) -> bool: if n == None or n.next == None: return False n.data = n.next.data n.next = n.next.next return True # def delete_middle(lst: LinkedList): # if lst.head == None: # return # p1 = lst.head # p2 = lst.head # prev = None # while p2.next != None and p2.next.next != None: # prev = p1 # p1 = p1.next # p2 = p2.next.next # if prev != None: # prev.next = prev.next.next a = LinkedList([1, 2, 3, 4, 5, 6]) # delete_middle(a) delete_node(a.head.next.next) print(a) d = LinkedList(['a', 'b', 'c', 'd', 'e']) delete_node(d.head.next) print(d)
from package.LinkedList import LinkedList def is_palindrome(lst) -> bool: stack = [] slow = lst.head fast = lst.head while fast != None and fast.next != None: stack.append(slow.data) slow = slow.next fast = fast.next.next is_odd_elements = fast != None if is_odd_elements: slow = slow.next # skip the middle one while slow != None: if stack.pop() != slow.data: return False slow = slow.next return True print(is_palindrome(LinkedList([1, 2, 3, 2, 1]))) print(is_palindrome(LinkedList([1, 2, 3]))) print(is_palindrome(LinkedList([2, 2])))
p1 = lst.head p2 = get_kth(lst, k) if p2 == None: raise IndexError() while p2.next != None: p1 = p1.next p2 = p2.next return p1.data def get_kth(lst: LinkedList, k: int): runner = lst.head for i in range(k - 1): if runner == None: return None runner = runner.next return runner a = LinkedList([1, 2, 3, 4, 5, 6, 7]) print(get_kth_last(a, 1)) print(get_kth_last(a, 7)) try: print(get_kth_last(a, 8)) except IndexError as err: print("IndexError") b = LinkedList([4]) print(get_kth_last(b, 1))
return longer def get_tail_and_length(lst: LinkedList) -> (LinkedList.Node, int): n = lst.head length = 1 while n.next != None: n = n.next length += 1 return (n, length) def advance_nodes(head: LinkedList, k: int) -> LinkedList.Node: n = head for _ in range(k): n = n.next return n a = LinkedList([1, 2, 3, 4, 5]) b = LinkedList([7, 8]) b.head.next.next = a.head.next.next.next print(b) n = get_intersection(a, b) print(n.data) m = get_intersection(a, b) print(m.data)