Skip to content

Instantly share code, notes, and snippets.

View vamsitallapudi's full-sized avatar
🚩
Become better than yesterday

Vamsi Tallapudi vamsitallapudi

🚩
Become better than yesterday
View GitHub Profile
class Node:
def __init__(self, data):
self.data = data # assigning the data passed
self.next = None # initializing the node as null
class LinkedList:
def __init__(self):
self.head = None
class Node:
def __init__(self, data):
self.data = data # assigning the data passed
self.next = None # initializing the node as null
class LinkedList:
def __init__(self):
self.head = None
# insertion of new node at front of linked list
def insert_at_front(head, data):
# creating new node to be inserted
new_node = Node(data)
new_node.next = head # pointing the new node's next to head
head = new_node # making the new node as head
return head
def insert_at_end(head, data):
new_node = Node(data)
current = head
while current.next: # traversing till the last node
current = current.next
# change the next of last node
current.next = new_node
return head
def insert_at_pos(head, data, position):
# edge case: check if pos is 0
new_node = Node(data)
if position is 0:
new_node.next = head
head = new_node
return head
pos = 0
new_node = Node(data)
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
def delete_at_start(head):
# edge case: return if head is null
if not head:
return None
head = head.next # moving head to next node
return head
def delete_at_end(head):
if not head:
return None
prev_node = head
while prev_node.next.next:
prev_node = prev_node.next
prev_node.next = None
return head
def delete_at_pos(head, position):
if not head:
return None
if position is 0:
return head.next
prev_node = head
curr_node = head.next
current_pos = 1
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
"""
Deleting a node at start.
"""
def delete_at_start(head):