Advertisement
AquaBlitz11

Linked List (iterative, no tails)

Jul 15th, 2020
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. class Node:
  2.  
  3.     def __init__(self, val, nxt=None):
  4.         self.val = val
  5.         self.nxt = nxt
  6.  
  7. class LinkedList:
  8.  
  9.     # construct an empty singly linked list (no tails)
  10.     def __init__(self):
  11.         self.head = None
  12.         self.count = 0
  13.    
  14.     def get_count(self):
  15.         return self.count
  16.    
  17.     def is_empty(self):
  18.         return (self.get_count() == 0)
  19.    
  20.     def __str__(self):
  21.         ret = []
  22.         curr = self.head
  23.         while curr != None:
  24.             ret.append(curr.val)
  25.             curr = curr.nxt
  26.         return str(ret)
  27.    
  28.     def get_node_at(self, idx):
  29.         if idx >= self.get_count() or idx < -self.get_count():
  30.             raise IndexError("list index out of range")
  31.         if idx < 0: # negative index = start counting from the back
  32.             idx = self.get_count()+idx
  33.         curr = self.head
  34.         for i in range(idx):
  35.             curr = curr.nxt
  36.         return curr
  37.    
  38.     def __getitem__(self, idx):
  39.         return (self.get_node_at(idx)).val
  40.    
  41.     def insert_front(self, val):
  42.         self.count += 1
  43.         self.head = Node(val, self.head)
  44.    
  45.     def insert_back(self, val):
  46.         if self.is_empty():
  47.             self.head = Node(val, None)
  48.             self.count += 1
  49.             return
  50.         curr = self.head
  51.         while curr.nxt != None:
  52.             curr = curr.nxt
  53.         curr.nxt = Node(val, None)
  54.         self.count += 1
  55.    
  56.     def delete_front(self):
  57.         if self.is_empty():
  58.             raise IndexError("deleting from an empty list")
  59.         self.head = self.head.nxt
  60.         self.count -= 1
  61.    
  62.     def delete_at(self, idx):
  63.         if self.is_empty():
  64.             raise IndexError("deleting from an empty list")
  65.         if idx >= self.get_count() or idx < -self.get_count():
  66.             raise IndexError("list index out of range")
  67.         if idx == 0 or idx == -self.get_count():
  68.             self.delete_front()
  69.             return
  70.         curr = self.get_node_at(idx-1)
  71.         curr.nxt = curr.nxt.nxt
  72.         self.count -= 1
  73.    
  74.     def delete_back(self):
  75.         self.delete_at(-1)
  76.     # def delete_back(self):
  77.     #     if self.is_empty():
  78.     #         raise IndexError("deleting from an empty list")
  79.     #     if self.get_count() == 1:
  80.     #         self.head = None
  81.     #         self.count = 0
  82.     #         return
  83.     #     curr = self.get_node_at(-2)
  84.     #     curr.nxt = None
  85.     #     self.count -= 1
  86.    
  87.     def reverse(self):
  88.         if self.get_count() <= 1:
  89.             return
  90.         curr = self.head
  91.         f1 = curr.nxt
  92.         f2 = f1.nxt
  93.         self.head.nxt = None
  94.         while f2 != None:
  95.             f1.nxt = curr
  96.             curr = f1
  97.             f1 = f2
  98.             f2 = f2.nxt
  99.         f1.nxt = curr
  100.         self.head = f1
  101.  
  102. # L = LinkedList()
  103. # L.insert_back(5)
  104. # L.insert_back(6)
  105. # L.insert_back(7)
  106. # print(L)
  107. # L.reverse()
  108. # print(L)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement