class Stack(object): def __init__(self): self.items = LinkedList() def __str__(self): s = ', '.join(map(str, self.items)) return 'Stack(>[' + s + '])' def __len__(self): return len(self.items) def isEmpty(self): return len(self) == 0 def push(self, item): self.items.appendleft(item) def pop(self): try: return self.items.popleft() except KeyError: return None def peek(self): return self.items[0] def clear(self): while not self.isEmpty(): self.pop()
def merge_linkedlists(alist: LinkedList, blist: LinkedList) -> LinkedList: newlist = LinkedList() p1, p2 = alist.head, blist.head k1, k2 = 0, 0 while p1.next and p2.next: v1, v2 = p1.next.data, p2.next.data if v1 >= v2: newlist.append(v1) p1 = p1.next k1 += 1 else: newlist.append(v2) p2 = p2.next k2 += 1 if p1 is not None and p2 is None: newlist.tail.next = p2 newlist.tail = blist.tail newlist._size += len(blist) - k2 elif p1 is None and p2 is not None: newlist.tail.next = p1 newlist.tail = alist.tail newlist._size += len(alist) - k1 return newlist
def reverse_linkedlist(alist: LinkedList) -> LinkedList: alist.tail = alist.head.next p = None while alist.head.next is not None: tmp = alist.head.next alist.head.next = tmp.next tmp.next = p p = tmp alist.head.next = p return alist
class Queue(object): def __init__(self): self.items = LinkedList() def __str__(self): s = ', '.join(map(str, self.items)) return f'Queue([{s}]<)' def __len__(self): return len(self.items) def enqueue(self, item): """Items enqueue in the rear(tail) of the list""" self.items.append(item) def dequeue(self): """Items dequeue from the front(head) of the list""" return self.items.popleft() def isEmpty(self): return len(self) == 0
""" ★ Task: To reverse a linkedlist ★ """ from datastruct.collections import LinkedList def reverse_linkedlist(alist: LinkedList) -> LinkedList: alist.tail = alist.head.next p = None while alist.head.next is not None: tmp = alist.head.next alist.head.next = tmp.next tmp.next = p p = tmp alist.head.next = p return alist if __name__ == '__main__': l = LinkedList([0, 3, 2, 4]) print(reverse_linkedlist(l))
""" ★ Task: Find the middle node of a LinkedList ★ Idea: Set two pointers, the second pointer takes twice the steps as the first one does. When the second reaches the end, the first points the middle node. """ from datastruct.collections import LinkedList def middle_node(alist: LinkedList): p1 = p2 = alist.head.next while p2 is not None and p2.next is not None: p1 = p1.next p2 = p2.next.next return p1.data if __name__ == '__main__': l = LinkedList([1, 2, 3, 4, 5, 6, 7, 8, 9]) print(middle_node(l))
while p1.next and p2.next: v1, v2 = p1.next.data, p2.next.data if v1 >= v2: newlist.append(v1) p1 = p1.next k1 += 1 else: newlist.append(v2) p2 = p2.next k2 += 1 if p1 is not None and p2 is None: newlist.tail.next = p2 newlist.tail = blist.tail newlist._size += len(blist) - k2 elif p1 is None and p2 is not None: newlist.tail.next = p1 newlist.tail = alist.tail newlist._size += len(alist) - k1 return newlist if __name__ == '__main__': a = LinkedList([6, 3, 1]) b = LinkedList([10, 9, 2, 0, -2]) c = merge_linkedlists(a, b) print(c) print(c[2:-1])
def __init__(self): self.items = LinkedList()