Тёмный

Swap Nodes in Pairs - Apple Interview Question - Leetcode 24 

NeetCode
Подписаться 807 тыс.
Просмотров 61 тыс.
50% 1

Опубликовано:

 

6 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 50   
@NeetCode
@NeetCode 3 года назад
Linked List Playlist: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-G0_I-ZF0S38.html
@wlcheng
@wlcheng 2 года назад
I have been watching your solutions for several months and this time I can draw the exactly same solution as you. So fulfilling! Thanks for your great work!
@kewtomrao
@kewtomrao 2 года назад
Theres no way i am coming up with this logic. Have solved about 400+ problems, but this level of thing is just too much!
@vdyb745
@vdyb745 2 года назад
This is so tricky and yet you have made it look simple !!! Fantastic .... Thank you so very much !!!!!!!
@CrazyzzzDudezzz
@CrazyzzzDudezzz 2 года назад
Wonderful explanation, if someone asked me to come up with this in 30 minutes I would walk out
@DmitriyKl
@DmitriyKl 2 года назад
Thank you! This took a couple of rewatches and I had to draw out my own list, but made sense after all!
@orellavie6233
@orellavie6233 2 года назад
I agree about dummy idea, but it always gonna point to 1 in the example, why it is accepted? dummy stays the same even if prev is changing to curr, but we had to update dummy.next just once to second with a flag or something. Very weird
@divyanshkumar9626
@divyanshkumar9626 2 года назад
exactly
@535_anshuj9
@535_anshuj9 7 месяцев назад
Why do we use a dummy node and not something like nullptr? (I apologize if i sound stupid)
@gatusko123
@gatusko123 Год назад
I was struggling a lot with this problem. More about the description of the problem I was thinking of swapping every two nodes. For example swap 1->2->3->4->5 Swap 1 with 3 and 3 with 5. But in reality was swamping 1 with 2 and change the pointer of those nodes. Man I was so dumb thank you for explanation.
@testbot6899
@testbot6899 2 года назад
Recursive solution : if not (head and head.next): return head head.next.next, head.next, head = head, self.swapPairs(head.next.next), head.next return head
@yilmazbingol4838
@yilmazbingol4838 3 года назад
I am confused why 'dummy["next"]' does not change. Because we assign prev=dummy. Since we both referencing the same memory address, if we mutate "prev", "dummy" will be mutated too. I even tested myself on jupyter, and "dummy.next" changes :)
@handlerhandle123
@handlerhandle123 2 года назад
The nodes are just pointers. So initially, dummy and prev *point* at the same thing. When you do prev = curr, you are telling prev to point to the memory location that curr is pointing to, but dummy is still pointing where it originally was pointing.
@muskanmall4401
@muskanmall4401 7 месяцев назад
@@handlerhandle123 which is node with value 1 right?
@WaldoTheWombat
@WaldoTheWombat 5 месяцев назад
This is how I did it: def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: if head and head.next: first = head previous = ListNode(69) head = head.next # save the second node in "head" for the return statement while first and first.next: # declare second = first.next third = second.next # switch second.next = first first.next = third # connect to previous previous.next = second # save previous previous = first # iterate to next pair first = third return head
@Ishaana3290
@Ishaana3290 Год назад
After the first pair of swapping where is the pointer location I mean where is prev , at 1 ???
@RGB_321
@RGB_321 Год назад
oh my this problem really fucked up my head last night
@Pwn-ki8zb
@Pwn-ki8zb Год назад
same
@Aditya.Rawat45
@Aditya.Rawat45 4 месяца назад
Bro, I have solved like 140 questions,but most of the time I'm unable to solve questions even after trying for 30-40 minutes.in the end i have to look at somebody's solution. How do i build my thinking process?how to improve
@user-nj8lu8ld9e
@user-nj8lu8ld9e 3 месяца назад
I'm in the exact same boat as you. take his solution, put it into python tutor, and go line by line, and draw pictures. don't leave a single thing unclear. there are lots of assumptions made.
@maskachung123
@maskachung123 Год назад
i think there is a problem in the drawing that is after swaping, the prev is pointed to the second position of the list, which is 1, not 3, and the curr is pointed to the 3 instead of 4, thats why it is confusing
@ForCodingInterview
@ForCodingInterview 2 года назад
Thanks NeetCode for the wonderful explanation
@LeonLeonLeonardo
@LeonLeonLeonardo Год назад
class Solution { public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) return head; swap(head); swapPairs(head.next.next); return head; } private void swap(ListNode head) { int nextValue = head.next.val; head.next.val = head.val; head.val = nextValue; } }
@MP-ny3ep
@MP-ny3ep Год назад
Beautiful explanation. Thanks
@user-kb2wd7iy7o
@user-kb2wd7iy7o Год назад
Solved the same using recursion.
@Jia-Tan
@Jia-Tan 8 месяцев назад
Can't we just use extra memory (array) for a problem like this? Makes it much simpler
@i_am_acai
@i_am_acai 3 года назад
why do we need a dummy node? why not just set directly to the second node and return that?
@handlerhandle123
@handlerhandle123 2 года назад
Having a dummy node makes the code easier to write (removes a lot of edge cases that are troublesome to write and think about) which is why a lot of LinkedList question solutions have dummy nodes added to the start of the list. You can do it without the dummy node, but it'll be more complicated code
@Tyler-jd3ex
@Tyler-jd3ex 2 года назад
I guess because None doesn't have a next property
@callmebiz
@callmebiz 3 года назад
Awesome explanation, subscribed :)
@halahmilksheikh
@halahmilksheikh 2 года назад
In the drawing, prev is at the head but in the code, prev is at the dummy. Was that a mistake?
@chaoluncai4300
@chaoluncai4300 2 года назад
This is very wise ! Thx 🙏
@vividviking6187
@vividviking6187 2 года назад
I did this recursively by calling each pair in the function. I am wondering about the Big O effectiveness. Would this be O (log N) since the function wouldn't be call for every node, but once for every pair?
@teamnitrogen210
@teamnitrogen210 Год назад
Since you're doing once for every pair, it would be O(N/2) which is still considered O(N). This is because by calling on each pair, you're just dividing the number of elements by 2.
@vividviking6187
@vividviking6187 Год назад
@@teamnitrogen210 Thanks for this bro
@JD-om6zk
@JD-om6zk 2 года назад
I love neetcode :)
@surajpokhrel8820
@surajpokhrel8820 Год назад
I am so dumb 😥😥
@ultimStars
@ultimStars Год назад
i hate linkedlists
@farnazzinnah1256
@farnazzinnah1256 2 года назад
I am confused with the return dummy.next part
@handlerhandle123
@handlerhandle123 2 года назад
The nodes are just pointers. So initially, dummy and prev point at the same thing. When you do prev = curr, you are telling prev to point to the memory location that curr is pointing to, but dummy is still pointing where it originally was pointing. I personally name it as "newHead" instead of "dummy". And in most solutions, you do return newHead.next when you want to return the head.
@aynuayex
@aynuayex 8 месяцев назад
dummy.next finally pointing to 2 is insane. since initially the given head is at 1 and we initialized dummy's next pointer with it.then without updating the dummy.next at least once how could it would be pointing to 2???
@shiladityabiswas2803
@shiladityabiswas2803 3 года назад
Instead of changing the pointers, why can't we just swap the values only, for every node pair.
@NeetCode
@NeetCode 3 года назад
You're correct, that solution is most efficient and easiest to implement. But in the problem statement they require that we are not allowed to change the values, i guess we can think of the node values as Read-Only memory.
@shiladityabiswas2803
@shiladityabiswas2803 3 года назад
@@NeetCode I see. Thanks for the quick update.
@sajalsharma5330
@sajalsharma5330 2 года назад
class Solution: def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode(0,head) prev = dummy curr = head while curr and curr.next: #save ptrs print("dummy1",dummy) nextpair = curr.next.next print("dummy2",dummy) second = curr.next print("dummy3",dummy) # reverse this ptr curr.next = nextpair print("dummy4",dummy) second.next = curr print("dummy5",dummy) prev.next = second print("dummy6",dummy) #update Ptr prev = curr print("dummy7",dummy) curr = nextpair print("dummy8",dummy) dummy1 ListNode{val: 0, next: ListNode{val: 1, next: ListNode{val: 2, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}}} dummy2 ListNode{val: 0, next: ListNode{val: 1, next: ListNode{val: 2, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}}} dummy3 ListNode{val: 0, next: ListNode{val: 1, next: ListNode{val: 2, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}}} dummy4 ListNode{val: 0, next: ListNode{val: 1, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}} dummy5 ListNode{val: 0, next: ListNode{val: 1, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}} dummy6 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}}} dummy7 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}}} dummy8 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}}} dummy1 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}}} dummy2 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}}} dummy3 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 3, next: ListNode{val: 4, next: None}}}}} dummy4 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 3, next: None}}}} dummy5 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 3, next: None}}}} dummy6 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 4, next: ListNode{val: 3, next: None}}}}} dummy7 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 4, next: ListNode{val: 3, next: None}}}}} dummy8 ListNode{val: 0, next: ListNode{val: 2, next: ListNode{val: 1, next: ListNode{val: 4, next: ListNode{val: 3, next: None}}}}} can anyone please explain .. why value of dummy 4 and dummy5 changing this way? tried whole day but didnt get it. Thanks in advance
@yogeshverma9267
@yogeshverma9267 2 года назад
In this intermediate state, the link between 1 and 2 breaks, and 1 directly points to 3(i.e. nextPair). We get it back in the link by pointing PREV to 2(i.e. second)
@akashmishra3369
@akashmishra3369 Год назад
Man I did the exact same thing but made an error in iteration :(
@akashmishra3369
@akashmishra3369 Год назад
couldnt figure out that prev = cur :/
@aishwaryasingla2781
@aishwaryasingla2781 10 месяцев назад
aws😃
@sarkersaadahmed
@sarkersaadahmed 10 месяцев назад
can anyone explain it a bit better please
Далее