Leetcode 2. Add Two Numbers | Add digits of two linked lists and return their sum | Linked List

preview_player
Показать описание

Check out our other playlists:

Dynamic Programming:

Trees:

Heaps and Maps:

Arrays and Maths:

Bit Manipulation:

Greedy Algorithms:

Sorting and Searching:

Strings:

Linked Lists:

Stack and Queues:

Two Pointers:

Graphs, BFS, DFS:

Backtracking:

Non- DSA playlists:

Probability:

SQL-Basic Join functions:

SQL-Basic Aggregate functions:
Рекомендации по теме
Комментарии
Автор

You can avoid some code duplication by doing like this:

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

ListNode l3 = new ListNode(-1);
ListNode l3Head = l3;
int carry = 0;

while (l1 != null || l2 != null){

int l1Val = 0;
if (l1 != null) {
l1Val = l1.val;
l1 = l1.next;
}

int l2Val = 0;
if (l2 != null){
l2Val = l2.val;
l2 = l2.next;
}

int value = l1Val + l2Val + carry;
l3.next = new ListNode(value % 10);
carry = value / 10;

l3 = l3.next;

}

if (carry != 0){
l3.next = new ListNode(carry);
}

return l3Head.next;

}

Автор

in depth knowledge of how to solve a particular problem and how to take care of edge cases. Best explanation ever <3

lifekool
Автор

really 😃 breaking down the problem into smaller chunks & solving every chunk 1 by 1 is the best thing of this channel.

ParthG-viml
Автор

I see many videos, but you explain very well. Best explanation and easy to understand.

RashidIqbal-ii
Автор

been looking several videos of this problem. This is by far the best explaination video I have ever seen. Finally someone who can shove down information in my brain. Thank you

DG_Theniel
Автор

Ma'am, I'm working with java but I got complete logic of the code Hats off to you Ma'am

SleepyBaseball-tsse
Автор

100 point for you. Love you very much for explain so eassy to understand

viencong
Автор

One of the best explanations I've seen. However, what is still throwing me off with LeetCode's solution was that they had l3 = head rather than head = l3 as you have here and I have been hung up on it for a while. I would appreciate it a lot if you could please help me understand why the order of the assignment doesn't seem to matter 🙏🙏🙏

alastairhewitt
Автор

i started coding last few weeks.your viedoes was so helpful to me .thank you

MdineshKumar-gpwg
Автор

You explained it in student way thank you for the simple logic ! A lots of love

rakeshruler
Автор

Thank you so much Alisha, your code and explanation was so easy to understand.

namrathas
Автор

thank you so much for this great explanation....I soled this problem in 3 different ways but some test cases didn't ran...When i saw ur explanation it was super easy...thanks a lot for

abhi_jit_tripathy
Автор

so much clarity in your explanation, amazing

hemantdixit
Автор

but it is always recommended to do in constant space right ?

sreeharsha
Автор

ListNode* l3 = new ListNode(0);
int carry =0;
ListNode* head = l3;
while(l1 && l2)
{
int value = l1->val+l2->val+carry;
carry = value/10;
l3->next = new ListNode(value%10);
l3 = l3->next;
l1 = l1->next;
l2 = l2->next;
}

while(l1)
{
int value = l1->val+carry;
carry = value/10;
l3->next = new ListNode(value%10);
l3 = l3->next;
l1 = l1->next;

}

while(l2)
{
int value = l2->val+carry;
carry = value/10;
l3->next = new ListNode(value%10);
l3 = l3->next;
l2 = l2->next;

}

if(carry)
{
l3 ->next = new ListNode(carry);
}
return head->next;

probabilitycodingisfunis
Автор

your explanation is so smooth and clear.thanks.
can you plz mention time complexity with your solution..it would be very useful.

PRITYKUMARI-znro
Автор

Whats d point of doing that if we r taking new list
It is advisable to do in same list without taking any extra space

mayankjaiswal
Автор

I am begining to code and your video is helpful

syedmunawar
Автор

thankew thankew thankew ! itna ache se samjhane ke liye. i was stuck in this problem because of the corner cases. Baaki videos se kuch khaas samaj nahi aya but with this video i got it all in one go! replay bhi nahi karna pada and i wrote it in java. once again thanking you🫂... bhagwan aapko ek world tour gift kar de meri taraf se🙏

negi-ji
Автор

just loved the way you explained....❤❤

darshanpancholi