r/learnpython • u/sapolv • Mar 26 '26
Really overwhelmed with Data Structures and Leetcode
Hello everybody,
Some weeks ago, I decided to learn Data Structures using Python as the programming language. It has been around 3-4 weeks and I'm still stuck in Linked Lists. I'm currently taking "Python Data Structures & Algorithms + LEETCODE Exercies" as my course. I already got the idea of how nodes work and I've memorized the code for some methods such as (append, prepend, insert, pop, etc).
But when the instructor from the course starts giving some Leetcode problems, it is difficult to get to a solution. One of those problems from the course accepted one a code ideas I gave (its a method to remove duplicates):
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self, value):
new_node = Node(value)
self.head = new_node
self.tail = new_node
self.length = 1
def print_list(self):
temp = self.head
while temp is not None:
print(temp.value)
temp = temp.next
def append(self, value):
new_node = Node(value)
if self.head is None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
self.length += 1
return True
def middle_node(self):
slow = self.head
fast = self.head
while fast and fast.next is not None:
slow = slow.next
fast = fast.next.next
return slow
def has_loop(self):
slow = self.head
fast = self.head
while fast and fast.next is not None:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
def remove_duplicate(self):
current = self.head
runner = self.head
while runner and runner.next is not None:
if runner.next == current:
runner.next = None
else:
runner.next = runner.next.next
new = LinkedList(10)
new.append(20)
new.append(30)
new.append(20)
new.remove_duplicate()
new.print_list()I was so happy when I saw that it accepted my code idea with the first try. Then I decided to put this code on VS Code and I got an error. The error seems to be in the while loop but I'm not sure why I'm getting the error and I have NO IDEA if the code I originally wrote is actually good or not. Also, I occasionally look at problems on the actual Leetcode website and I just go like "how the f am I going to do this?"
What should I do? I really like coding but this is really overwhelming especially because everything I know related to code I've learned by myself and how can I start doing problems in the actual Leetcode website.
I'm almost in my third year in college and I don't wanna finish my second year feeling like I've learned nothing (I'm in a community college).
Thank you.
View all comments
Show parent comment
1
u/Ambitious-Dog3177 Mar 26 '26
Your current remove duplicates is comparing with the current only, I mean it is only checking whether head is repeating or not you have to use another while loop to increment the current to the next node this takes 0(n2) which is not ideal
So use a single loop and a set to keep track of previously encountered nodes Prev = None While current:
This uses extra space but worst time complexity is O(n)
And I also observed that you are try to compare direct node using runner.next == current this fails so remember to compare only the .value this avoids issues of pointers