LeetCode 2095
Linked lists can be intimidating when you first start learning Data Structures and Algorithms. You can't just jump to the middle of the list like you can with a standard array; you have to traverse it step-by-step.
Today, we are going to break down LeetCode 2095: Delete the Middle Node of a Linked List. We will look at a simple, easy-to-understand Python solution and walk through it line-by-line so you can see exactly how it works.
Understanding the Problem
The problem gives us the head (the very first node) of a linked list. Our goal is to find the middle node, delete it, and return the modified list.
How do we define the "middle"? If the list has n nodes, the middle is the ⌊n / 2⌋th node (using 0-based indexing).
If your list has 5 nodes (Indices 0, 1, 2, 3, 4), the middle is
5 / 2 = 2. We want to delete the node at index 2.If your list has 4 nodes (Indices 0, 1, 2, 3), the middle is
4 / 2 = 2. We still want to delete the node at index 2.
head.next looks at the second node. If there is no second node (not head.next), the list size is 1. Since the middle of 1 node is that exact node itself, we delete it by simply returning None (an empty list).We create a variable
mid(which is actually acting as our total node counter here) and start it at0.We create a pointer
pand start it at theheadof the list.The
while p:loop moves our pointer forward one step at a time (p = p.next) and adds 1 to our counter (mid = mid + 1) until we hit the end of the list. By the end of this loop,midholds the total number of nodes.
To delete a node in a linked list, you can't just stand on the node you want to delete. You have to stand on the node right before it so you can change where its "next" pointer points.
mid // 2gives us the exact index of the middle node.Subtracting
1(mid // 2 - 1) gives us the number of steps we need to take from theheadto land exactly one node before the middle.We overwrite our
midvariable with this new target number, and we reset ourppointer back to theheadto start our second journey.
p = p.next), we subtract 1 from mid. When mid hits 0, the loop stops. At this exact moment, p is pointing to the node immediately preceding our middle node.This is the magic line of linked lists.
Currently, p is pointing to the middle node. We simply tell p to let go of the middle node, and instead, grab the hand of the node after the middle one (p.next.next).
Because nothing is pointing to the middle node anymore, it is effectively deleted from our chain!
Note: Finally, we
return headto give back our brand new, modified linked list!
Complexity Check
Time Complexity: O(N). We iterate through the list one and a half times (once fully to count, and once halfway to delete). In Big O notation, we drop the constants, leaving us with a linear time complexity of O(N).
Space Complexity: O(1). We are only using two variables (
midandp) to keep track of numbers and positions. We aren't creating any new arrays or data structures, so our memory usage is constant.
Comments
Post a Comment
if you have any doubts let me know.