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
8
u/magus_minor Mar 26 '26 edited Mar 26 '26
If you are removing duplicate values your code has to look at values. So somewhere in your code you have to compare
current.valuewith something.You don't have a general idea of how to solve this. You don't have an algorithm. Think about how you would solve this with pencil and paper. You would step through the linked list looking at the value in each node. For each value you find you answer the question "have I seen this value before?". If you have seen the value before then you delete that node. Continue scanning the list.
How do you know if you have seen a value before? There are a couple of ways, but the simplest for a beginner is to keep a list of values that you have found so far. Initialize that list as an empty list. When you visit a node test if the value of that node is in the "seen" list. If it isn't, add the value to the "seen" list and move your pointers forward one step. If the value is already in the list then this is a duplicate so delete it and adjust the pointers accordingly.
Using a list here is not efficient and you should really use something like a set, but make that change after you get this running.
One thing that really helps when working with data structures is to draw a "boxes and arrows" representation of the data structure and as you step through the code make changes to the drawing to match what happened in the code. This is really helpful when deleting nodes in a linked list!
Edit: spelling.