kill_-9

Untitled

Nov 6th, 2022
1,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. from typing import Any, Optional
  2.  
  3.  
  4. class Node:
  5.     def __init__(self, value: Optional[Any] = None, next: Optional['Node'] = None) -> None:
  6.         self.value = value
  7.         self.next = next
  8.  
  9.     def __str__(self) -> str:
  10.         return 'Node [{value}]'.format(value=str(self.value))
  11.  
  12.     def set_next(self, newnext):
  13.         self.next = newnext
  14.         return self.next
  15.  
  16. class LinkedList:
  17.     def __init__(self) -> None:
  18.         self.head: Optional['Node'] = None
  19.         self.length = 0
  20.  
  21.     def __str__(self) -> str:
  22.         if self.head is not None:
  23.             current = self.head
  24.             values = [str(current.value)]
  25.             while current.next is not None:
  26.                 current = current.next
  27.                 values.append(str(current.value))
  28.             return '[{values}]'.format(values=' '.join(values))
  29.         return 'LinkedList []'
  30.  
  31.     def append(self, elem: Any) -> None:
  32.         new_node = Node(elem)
  33.         if self.head is None:
  34.             self.head = new_node
  35.             return
  36.  
  37.         last = self.head
  38.         while last.next:
  39.             last = last.next
  40.         last.next = new_node
  41.         self.length += 1
  42.  
  43.     def remove(self, index) -> None:
  44.         cur_node = self.head
  45.         cur_index = 0
  46.         if self.length == 0 or self.length <= index:
  47.             raise IndexError
  48.         if cur_node is not None:
  49.             if index == 0:
  50.                 self.head = not cur_node.next
  51.                 self.length -= 1
  52.                 return
  53.  
  54.         while cur_node is not None:
  55.             if cur_index == index:
  56.                 break
  57.             prev = cur_node
  58.             cur_node = cur_node.next
  59.             cur_index += 1
  60.  
  61.         prev.next = cur_node.next
  62.         self.length -= 1
  63.  
  64.     def get(self, index: int) -> Node:
  65.         cur_node = self.head
  66.         cur = cur_node.next
  67.         if index is None:
  68.             while cur is not None:
  69.                 if cur.next is None:
  70.                     raise IndexError
  71.                     #return cur
  72.                 cur_node = cur
  73.                 cur = cur.next
  74.             self.head = None
  75.             return cur_node
  76.  
  77.         cur_index = 0
  78.         while cur_node is not None:
  79.             if cur_index == index - 1:
  80.                 cur_node.set_next(cur.next)
  81.                 return cur
  82.             prev = cur_node
  83.             cur_node = cur_node.next
  84.             cur_index += 1
  85.  
Advertisement
Add Comment
Please, Sign In to add comment