Linked Lists Flashcards
Linked List node class constructor code
class Node: def \_\_init\_\_(self, value): self.value = value self.next = None
Linked List constructor code
class LinkedList: def \_\_init\_\_(self, value): new_node = Node(value) self.head = new_node self.tail = new_node self.length = 1
Linked List print list code
def print_list(self): temp = self.head while temp is not None: print(temp.value) temp = temp.next
Linked List append code
def append(self, value): new_node = Node(value) if self.length == 0: self.head = new_node self.tail = new_node else: self.tail.next = new_node self.tail = new_node self.length += 1 return True
Linked List pop code
def pop(self): if self.length == 0: return None temp = self.head pre = self.head while(temp.next): pre = temp temp = temp.next self.tail = pre self.tail.next = None self.length -= 1 if self.length == 0: self.head = None self.tail = None return temp
Linked List prepend code
def prepend(self, value): new_node = Node(value) if self.length == 0: self.head = new_node self.tail = new_node else: new_node.next = self.head self.head = new_node self.length += 1 return True
Linked List pop_first code
def pop_first(self): if self.length == 0: return None temp = self.head self.head = self.head.next temp.next = None self.length -= 1 if self.length == 0: self.tail = None return temp
Linked List get code
def get(self, index): if index < 0 or index >= self.length: return None temp = self.head for _ in range(index): temp = temp.next return temp
Linked List set value code
def set_value(self, index, value): temp = self.get(index) if temp: temp.value = value return True return False
Linked List insert code
def insert(self, index, value): if index < 0 or index > self.length: return False if index == 0: return self.prepend(value) if index == self.length: return self.append(value) new_node = Node(value) temp = self.get(index - 1) new_node.next = temp.next temp.next = new_node self.length += 1 return True
Linked List remove code
def remove(self, index): if index < 0 or index >= self.length: return None if index == 0: return self.pop_first() if index == self.length - 1: return self.pop() pre = self.get(index - 1) temp = pre.next pre.next = temp.next temp.next = None self.length -= 1 return temp
Linked List reverse code
def reverse(self): temp = self.head self.head = self.tail self.tail = temp after = temp.next before = None for _ in range(self.length): after = temp.next temp.next = before before = temp temp = after
Removing an item from the tail end of a Linked List is which Big O?
O(n)
Because you have to start at the beginning of the Linked List an iterate through to the end
Removing an item from the beginning of a Linked List is which Big O?
O(1)
This is a place where Linked Lists are better than Lists. Lists are O(n) when removing the first item because of the reindexing that is required.
Finding an item by index in a Linked List is which Big O?
O(n)
You have to iterate through the Linked List until you get to the index you are looking for.