Merge Nodes in Between Zeros - Leetcode 2181 - Python

  Рет қаралды 6,307

NeetCodeIO

NeetCodeIO

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
🐦 Twitter: / neetcode1
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
Problem Link: leetcode.com/problems/merge-n...
0:00 - Read the problem
0:20 - Drawing Explanation 1
3:10 - Coding Explanation 1
6:48 - Drawing Explanation 2
8:12 - Coding Explanation 2
leetcode 2181
#neetcode #leetcode #python

Пікірлер: 21
@coderunner743
@coderunner743 Ай бұрын
thanks, sir because of you I got placed in Zoho.
@NeetCodeIO
@NeetCodeIO 29 күн бұрын
Congratulations so happy to hear that :)
@arunmurugan7093
@arunmurugan7093 28 күн бұрын
I just want to say that ever since I started following your youtube channel and videos, I have been having an easier time coming up with solutions to a problem. Everytime before I watch your videos, I try to think of solution(s) and then I come here to see if I was on the right track and I have been doing great! It's great to see myself improve and it has all been because of your videos and my practice! Thank you for these videos and keep it up!
@AJK-a2j00k4
@AJK-a2j00k4 29 күн бұрын
2nd approach is on different level!
@user-nt4se7vc8v
@user-nt4se7vc8v Ай бұрын
Thanks, sir Could you please tell me the toll you use to take notes?
@yang5843
@yang5843 Ай бұрын
Thanks for uploading I was curious about the in place solution
@galkk3
@galkk3 Ай бұрын
my in-place solution: prev = head cur = head.next while cur: temp = 0 prev = prev.next while cur.val != 0: temp += cur.val cur = cur.next cur = cur.next prev.val = temp prev.next = cur return head.next
@shwethaks7994
@shwethaks7994 29 күн бұрын
@NeetCodeIO can you make a video on giving tips regarding job search and some websites where we can apply. I think this would be very helpful to many people like me in the middle of a job hunt.
@kaunasmiit246
@kaunasmiit246 Ай бұрын
I am recursively calling for each 0
@nickleo4308
@nickleo4308 Ай бұрын
crazy🥵
@nirmalgurjar8181
@nirmalgurjar8181 Ай бұрын
5:13, we can initialize cur = head.next in the start to avoid first 0 and only update return list when we get node.val == 0 instead checking node.next.val == 0, to reduce complexity and typo and potential bug in the code.
@beinghappy9223
@beinghappy9223 Ай бұрын
class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: temp = head ans = ListNode(0) dummy = ans curr_sum = 0 while temp: if temp.val == 0: if curr_sum >0: dummy.next = ListNode(curr_sum) dummy = dummy.next curr_sum = 0 else: curr_sum += temp.val temp = temp.next return ans.next
@chien-yuyeh9386
@chien-yuyeh9386 Ай бұрын
🥳
@zongyu100
@zongyu100 Ай бұрын
It seems like your in-place solution is still a bit complicated, could have just keep adding node values to the 0s and delete the last 0, since when you reaching right before the last 0, you could delete the last 0 by "if not cur.next.next: cur.next = cur.next.next"
@tuandino6990
@tuandino6990 Ай бұрын
I did it without any knowledges regarding linked list, and after reading my code I dont know why it works
@mohanedomer9081
@mohanedomer9081 Ай бұрын
second comment
@TheSambitDutta
@TheSambitDutta Ай бұрын
Very confusing explanation!! Like if you agree...
@yang5843
@yang5843 Ай бұрын
My new list solution is simpler, skip the first 0, add a new node when we hit a 0 class Solution { public ListNode mergeNodes(ListNode head) { ListNode rc = new ListNode(); head = head.next; ListNode copy = rc; int cur = 0; while ( head!=null ) { if ( head.val == 0 ) { rc.next = new ListNode(cur); rc = rc.next; cur = 0; } cur += head.val; head = head.next; } return copy.next; } }
@krityaan
@krityaan 29 күн бұрын
Using additional memory (creating a new node) could be viewed as inefficient during an interview. If you can't modify the original linked list, then this is preferred, but the challenge is to optimise time and space complexities
@mohammedsuhail.s192
@mohammedsuhail.s192 Ай бұрын
# Definition for singly-linked list. class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: li=[] li1=[] while head: li.append(head.val) head=head.next i=0 head=None while i
@maestr3218
@maestr3218 Ай бұрын
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode mergeNodes(ListNode head) { ListNode ansNode = new ListNode(0); ListNode dummy = ansNode; if(head == null) return null; ListNode flag = head; while(flag != null) { boolean present = false; int sum = 0; ListNode temp = flag; while(temp.val != 0) { present = true; sum += temp.val; temp = temp.next; } if(present = true) { ListNode node = new ListNode(sum); dummy.next = node; dummy = node; } flag = temp.next; } return ansNode.next.; } } can somone help ?? why my ans list is 0->4->11 for the first testcase even when im returning ansnode.next
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 16 МЛН
Inside Out 2: Who is the strongest? Joy vs Envy vs Anger #shorts #animation
00:22
What it feels like cleaning up after a toddler.
00:40
Daniel LaBelle
Рет қаралды 87 МЛН
Nastya and SeanDoesMagic
00:16
Nastya
Рет қаралды 38 МЛН
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 385 М.
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 595 М.
The Man Who Solved the World’s Most Famous Math Problem
11:14
Newsthink
Рет қаралды 758 М.
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
Rob Mulla
Рет қаралды 265 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 392 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
5 Uncommon Python Features I Love
15:09
Indently
Рет қаралды 142 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 152 М.
Better Than Smart Phones☠️🤯 | #trollface
0:11
Not Sanu Moments
Рет қаралды 16 МЛН
Looks very comfortable. #leddisplay #ledscreen #ledwall #eagerled
0:19
LED Screen Factory-EagerLED
Рет қаралды 9 МЛН
low battery 🪫
0:10
dednahype
Рет қаралды 1,6 МЛН