Paste Search Dynamic
Recent pastes
Node LinkedList
  1. # A single node of a singly linked list
  2. class Node:
  3.   # constructor
  4.   def __init__(self, data = none, next=none):
  5.     self.data = data
  6.     self.next = next
  7.  
  8. # A Linked List class with a single head node
  9. class LinkedList:
  10.   def __init__(self):  
  11.     self.head = none
  12.  
  13.   # insertion method for the linked list
  14.   def insert(self, data):
  15.     newNode = Node(data)
  16.     if(self.head):
  17.       current = self.head
  18.       while(current.next):
  19.         current = current.next
  20.       current.next = newNode
  21.     else:
  22.       self.head = newNode
  23.      
  24.  
  25.   def delete(self,data):
  26.     current= self.head
  27.     if(current.data==data):
  28.         self.head = current.next
  29.     if(current.next.data == data):
  30.       if(current.next.next != none):
  31.         current.next = current.next.next
  32.       else:            
  33.         current.next = none
  34.     else:
  35.       current = current.next
  36.  
  37.   # print method for the linked list
  38.   def printLL(self):
  39.     current = self.head
  40.     while(current):
  41.       print(current.data)
  42.       current = current.next
  43.  
  44. # Singly Linked List with insertion and print methods
  45. LL = LinkedList()
  46. LL.insert(3)
  47. LL.insert(4)
  48. LL.insert(5)
  49. LL.delete(5)
  50. LL.printLL()
Parsed in 0.016 seconds