Videos
Please help me understanding Linked List in Python in LeetCode
How to understand the data structure of Python Linked List in Leetcode - Stack Overflow
list - Leetcode Python Issue - Stack Overflow
Any disadvantages to using Python when solving Leetcode problems?
Hello all,
I learned Data Structures in C, which I understood pretty good, but can't wrap around about them in Python as I am relatively new to DS in Python. I solved some DS questions in C before but I don't want to go back to C every time I get a DS problem. What needs to be done in this problem is to remove nth node from end of the linked list. What I wanted to do is to count total nodes and find the position from the start of the list and traverse two pointers, one which points the actual node to be bypassed/deleted and a previous node of which I will connect the "actual node"'s next node...but it doesn't seem to work.
Image for reference: https://ibb.co/jRy39k7
How I did is to make a curr pointer = head to count total nodes. After traversing, make a prev pointer = head and now set curr = head.next and traverse the list until the curr reaches the position required (which is given by i) and prev before curr.
Here are the things which I don't understand:
From what I understand, after the second while loop breaks, the curr is at node 4 with value 4 (shown in left top image) and prev is at node 3 and both (I think) clearly has a attribute called "val" but why is there an error in the left? You can see the values being printed below too.
You can also clearly see that there are only two print statements in the whole code but I am getting a total of 3 print outputs. Why is that?
Here's the code:
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
node_count = 0
curr = head
while True:
if curr is None:
break
node_count += 1
curr = curr.next
i = node_count-n-1
prev = head
curr = head.next
x = 0
while True:
if x >= i: break
curr = curr.next
prev = prev.next
x += 1
print(prev.val)
print(curr.val)
return headNOTE:
I clearly know that the code is incomplete, that I didn't actually delete the required node.
There maybe a better solution than mine but right now, I am only concerned with code to be working without any errors.
I have already tried using a for loop like for x in range(i) but same stuff.
Please help me, it would be a great favour.
Thank You
Drawing diagrams helps. Here's your linked list:
Copy[ ]
|
v
[ ]
|
V
[ ]
|
V
None
Each arrow leading from a box represents the next attribute of that node.
Here are the three variables a, b, and c:
Copy [ ] <-- a
|
v
[ ] <-- b
|
V
[ ] <-- c
|
V
None
Each of these variables also points to a particular node.
If you say b.next = None, the next attribute of the node referenced by b is modified, like this:
Copy [ ] <-- a
|
v
None <-- [ ] <-- b
[ ] <-- c
|
V
None
This modifies the structure of the list. If you just set b itself to a different value, though, this is what happens:
Copy [ ] <-- a
|
v
None <-- [ ] b --> None
[ ] <-- c
|
V
None
You changed b, but the node that b used to point to stays right where it was. Note that this is similar to how the c node continued to exist even after you set b.next = None.
Python doesn't have double pointers e.g. **x
Copyb.next = c
print(a) # a is 1 -> 2 -> 3
b.next = None
E.g. in above, it doesn't mean c is None
When a.next is b, if you change a.next.next you are effectively changing b.next
But if you change a.next to None, it will not set b to None
Edit:
Also when you set b = None but a.next still points ListNode(2)
Turns out the solution to this is to use: nums[:] = nums[newIndex+1:len(nums)] + nums[0:newIndex+1].
Doing nums = nums[newIndex+1:len(nums)] + nums[0:newIndex+1] simply changes the reference, while nums[:] changes the values of the list.
You can use
sudo service network-manager restart