Design Linked List - Leetcode 707 - Python

  Рет қаралды 28,351

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 31
@Javokh1r
@Javokh1r 6 ай бұрын
Creating extra dummy nodes is well-thought and makes the double linked list so easy to implement. Thanks a lot!
@voctor1381
@voctor1381 Жыл бұрын
Commenting as a surprise for myself in a few months during Leetcode season Happy Neetcoding everyone 👍
@amlivinginhell
@amlivinginhell Жыл бұрын
what happened?
@mannyw_
@mannyw_ Ай бұрын
💀
@PerseRos285
@PerseRos285 2 ай бұрын
A small improvement is to implement a bidirectional search to perform the get function faster. Basically, based on the index, we decide if is faster to move forward or backward (from the head/tail to the index-th position). if index < self.size // 2: current = self.head.next for _ in range(index): current = current.next else: current = self.tail.prev for _ in range(self.size - index - 1): current = current.prev return current
@mannyw_
@mannyw_ Ай бұрын
General idea behind the optimization is good, but this would fail if index is invalid
@maryabuor2824
@maryabuor2824 Жыл бұрын
This is very well explained, especially the illustrations. You've gained a new subscriber!
@vijayvarshinilakshminaraya887
@vijayvarshinilakshminaraya887 6 ай бұрын
The jump from ‘you can turn off your brain’ to ‘maybe you don’t have to turn off your brain too much’ had me😂😂😂😂😂😂😂
@krateskim4169
@krateskim4169 Жыл бұрын
your consistency is awesome
@LEGGOMYEGGO206
@LEGGOMYEGGO206 2 ай бұрын
man thanks for the breakdown i just started linked list's and I spent about a day trying to figure out how to solve this problem I would get down to like 2 edge cases that I couldn't solve, so thanks again for the vid
@Ziad.Dev99
@Ziad.Dev99 Жыл бұрын
Thanks a lot 👍 I think the main idea here is 'two dummy nodes', this makes me control the boundaries of linked list. this technique helps in many linked list problems.
@lembueno894
@lembueno894 11 ай бұрын
Nothing makes me more sad than after 2 hours of this trying to fix the 36th case, I give up and look this video up for a WAY more simpler and short solution. The count down loops would have helped :/
@kartikgadagalli1008
@kartikgadagalli1008 6 ай бұрын
The doubly linked list is easy to implement it's the edge cases where a lot of errors happen. The dummy nodes really make it easy to implement a doubly linked list.
@NikhilAsawadekar
@NikhilAsawadekar 3 ай бұрын
at 7:33, can we just assume that index is always going to be 0 if cur is not null and cur is not self.right? What't the need to check index == 0 in the while loop?
@podilichaitanyaakhilkumar4911
@podilichaitanyaakhilkumar4911 Жыл бұрын
Explained well 👏👏. I am a regular follower of your channel. I have gone through your playlist, but I could only find a few hard problems and many easy and medium problems. Upload more leetcode hard problems. It will be beneficial for cracking Faang companies. Thank you.
@Disusede
@Disusede Жыл бұрын
At the moment of writing this, this solution fails on Leetcode. For it to pass correctly you have to set a right boundary to the while loop list traversal by adding a `node != self.right` condition. Otherwise if you go out of bounds the code will try to access a .next property on a None value and will fail.
@Disusede
@Disusede Жыл бұрын
So for example the while loop condition in the get method will be: while cur and index > 0 and cur != self.right:
@AmitDeveloper-mg3kl
@AmitDeveloper-mg3kl Жыл бұрын
For me all the TC passed . I did not change the code but it works fine@@Disusede
@business_central
@business_central Жыл бұрын
Can anyone please tell me why in his last function: DeleteAtIndex only checked if curr != Self.right ? What about if the pointer is put on the left dummy node self.left ??
@staceyonuora5329
@staceyonuora5329 Жыл бұрын
The pointer would never be on the self.left dummy node. This is because he started the current node from self.left.next and moved further right from there on.
@pavanbandaru8180
@pavanbandaru8180 9 ай бұрын
He doesnt want to delete the dummy node
@manojrao9867
@manojrao9867 Ай бұрын
Wonderfull.
@vuejs1
@vuejs1 2 ай бұрын
that index -- trick in neet
@iftekharuddin
@iftekharuddin Жыл бұрын
💪
@MrCCronyn
@MrCCronyn 10 ай бұрын
This solution gives me a memory limit exceeded error. Does anyone know why?
@raz_dva_
@raz_dva_ Жыл бұрын
There is no Memory Leak issue here - both Java and Python will immediately collect node to garbadge as far as it is not referenced from main program grapth.
@ahmadmtera
@ahmadmtera Ай бұрын
The dummy nodes lead to wasted space. Imagine a user of your LinkedList class wants to make an array of LinkedLists, wherein each LinkedList is a LinkedList as well. The outer array would mean that there's a O(2n) wasted RAM just for the dummy nodes. That wouldn't be a good design, and using such strategy across solutions as a lazy shortcut to make the code easier is the wrong methodology - one should utilize the resources a program uses to provide the best program to the end user, not to the developer's ease of use. PS I was able to build a Doubly Linked List with not much more LOC compared to the video and there's no wasted memory.
@william.darrigo
@william.darrigo Жыл бұрын
cries in binary
@KevinDevZ
@KevinDevZ Жыл бұрын
Im early
@noob-xv9yr
@noob-xv9yr 2 ай бұрын
class Node: def __init__(self,val): self.val=val self.next=None self.prev=None class MyLinkedList(object): def __init__(self): self.head=Node(0) self.tail=Node(0) self.head.next=self.tail self.tail.prev=self.head def get(self, index): """ :type index: int :rtype: int """ curr=self.head.next while curr!=self.tail and index>0: curr=curr.next index-=1 if curr and curr!=self.tail and index==0: return curr.val return -1 def addAtHead(self, val): """ :type val: int :rtype: None """ node=Node(val) self.head.next.prev=node self.head.next=node node.prev=self.head return self.head.next def addAtTail(self, val): """ :type val: int :rtype: None """ node=Node(val) self.tail.prev.next=node node.prev=self.tail.prev node.next=self.tail self.tail.prev=node return self.tail.prev def addAtIndex(self, index, val): """ :type index: int :type val: int :rtype: None """ node=Node(val) curr=self.head.next while curr!=self.tail and index>0: curr=curr.next index-=1 if curr!=self.tail and index==0 : curr.prev.next=node node.prev=curr.prev node.next=curr curr.prev=node return curr.prev else: return False def deleteAtIndex(self, index): """ :type index: int :rtype: None """ curr = self.head.next # Start from the first real node # Traverse the list until the correct index or end of the list while curr != self.tail and index > 0: curr = curr.next index -= 1 # Ensure curr is not the dummy tail and index is valid if curr != self.tail and curr != self.head and index == 0: prev_node = curr.prev next_node = curr.next # Ensure next_node is not None before accessing its prev attribute if next_node: prev_node.next = next_node next_node.prev = prev_node # Your MyLinkedList object will be instantiated and called as such: # obj = MyLinkedList() # param_1 = obj.get(index) # obj.addAtHead(val) # obj.addAtTail(val) # obj.addAtIndex(index,val) # obj.deleteAtIndex(index) this code works but if i remove the if next_node: from deletAtindex we get typnone object does not have pre somethig like error why are we getting this is it because the leetcode system is expecting without dummy nodes and trying to delete a dummy node in edge cases ???
@pabloj1336
@pabloj1336 4 ай бұрын
Anyone else get AttributeError: 'NoneType' object has no attribute 'next' ^^^^^^^^^ prev.next = newNode Line 59 in addAtIndex (Solution.py) ^^^^^^^^^^^^^^^ result = obj.addAtIndex( Line 117 in __helper_select_method__ (Solution.py) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ret.append(__DriverSolution__().__helper_select_method__(method, params[index], obj)) Line 163 in _driver (Solution.py) _driver() Line 172 in (Solution.py) with this solution?
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 453 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 600 М.
啊?就这么水灵灵的穿上了?
00:18
一航1
Рет қаралды 51 МЛН
Every parent is like this ❤️💚💚💜💙
00:10
Like Asiya
Рет қаралды 26 МЛН
Стойкость Фёдора поразила всех!
00:58
МИНУС БАЛЛ
Рет қаралды 7 МЛН
Part 5. Roblox trend☠️
00:13
Kan Andrey
Рет қаралды 2,9 МЛН
Rotate List - Linked List - Leetcode 61 - Python
10:16
NeetCode
Рет қаралды 42 М.
Design a Singly Linked List
12:12
NeetCodeIO
Рет қаралды 20 М.
Learn Linked Lists in 13 minutes 🔗
13:24
Bro Code
Рет қаралды 316 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 259 М.
2 Simple Ways To Code Linked Lists In Python
10:55
Anthony Sistilli
Рет қаралды 76 М.
The LeetCode Fallacy
6:08
NeetCode
Рет қаралды 540 М.
Separate Black and White Balls - Leetcode 2938 - Python
8:15
NeetCodeIO
Рет қаралды 7 М.
Linked List - Beau teaches JavaScript
11:57
freeCodeCamp.org
Рет қаралды 99 М.
啊?就这么水灵灵的穿上了?
00:18
一航1
Рет қаралды 51 МЛН