Skip to content

Commit

Permalink
neetcode's solution -@iamserda
Browse files Browse the repository at this point in the history
  • Loading branch information
iamserda committed Jan 24, 2025
1 parent 8134824 commit d142bc3
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class ListNode:
def __init__(self, val):
self.val = val
self.next = None


# Implementation for Singly Linked List
class LinkedList:
def __init__(self):
# Init the list with a 'dummy' node which makes
# removing a node from the beginning of list easier.
self.head = ListNode(-1)
self.tail = self.head

def insertEnd(self, val):
self.tail.next = ListNode(val)
self.tail = self.tail.next

def remove(self, index):
i = 0
curr = self.head
while i < index and curr:
i += 1
curr = curr.next

# Remove the node ahead of curr
if curr and curr.next:
if curr.next == self.tail:
self.tail = curr
curr.next = curr.next.next

def print(self):
curr = self.head.next
while curr:
print(curr.val, " -> ", end="")
curr = curr.next
print()

0 comments on commit d142bc3

Please sign in to comment.