Add Two Numbers | Live Coding with Explanation | Leetcode - 2

preview_player
Показать описание
This question uses the basic approach to add two numbers stores as nodes of linked list in O(Max(m,n)) time and space.

To support us you can donate

Check out our other popular playlists:

If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful.

#coding #leetcode #programminglife #programmingisfun #programmer #tech #software #codinglife #leetcode
Рекомендации по теме
Комментарии
Автор

We hope you all are enjoying our videos!!! Don't forget to leave a comment!!! Please like the video to support us!!!

Questions you might like:

Struggling in a question??

Leave in a comment and we will make a video!!!🙂🙂🙂

AlgorithmsMadeEasy
Автор

wow, u deserve 1milion views, what an explanation, thank you very much

nagasivakrishna
Автор

Amazing video, keep up the great work! :)

ComputerScienceSimplified
Автор

For line 24 & 25, can we have "if (p.NEXT != null) p = p.next" instead?

yanshanwan
Автор

Your videos are really helpful...keep it going.

amlansarkar
Автор

Very nicely explained, thank you, teacher<3

gokulnaathb
Автор

carry = sum /10; -> 7/10 = 0.7;
why sum is saving -> sum %10 ??
any one know ?

worthyvibes
Автор

Your carry = sum/10 will always return some value.. So your line number 28 will always have a value right.

pradeepgjain
Автор

why carry is assigned to dummy??
carry=dummy; ??

divyaamohan
Автор

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// write a code
ListNode dummy = new ListNode(0);
ListNode p = l1;
ListNode q = l2;
ListNode curr = dummy;
int carry = 0;

while(p!=null || q!=null)
{
int x = p!=null ? p.val : 0;
int y = q!=null ? q.val : 0;
int sum= x+y+carry;
carry=sum/10;
curr.next = new ListNode(sum%10);
curr=curr.next;

if(p!=null)
p=p.next;

if(q!=null)
q=q.next;
}
if(carry>0)
{
curr.next=new ListNode(carry);
}
return dummy.next;
}
}

/**
* 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; }
* }
*/

shreeramkushwaha
Автор

i beat 100% of the users, with 1ms runtime

vishalsupv