LeetCode Solution - 2.0 Add Two Numbers | Google Interview

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ต.ค. 2019
  • This is a detail solution to question number 2 in LeetCode called Add Two Numbers. This question uses LinkLists. I think this is a great question, although i think it probably should be an easy instead of medium.
    Please feel free to drop any question or comments

ความคิดเห็น • 25

  • @roshankumarsharma8725
    @roshankumarsharma8725 2 ปีที่แล้ว +1

    Hey Man, I just want to appreciate your work! Thank you for all your efforts, this series is really a gem for me! love from India

  • @vivarantx
    @vivarantx 2 ปีที่แล้ว +1

    you are great man!....you explain things in a way that it becomes really easy to solve this stuff

  • @ucheuba8204
    @ucheuba8204 4 ปีที่แล้ว +1

    Great video man, keep them coming. Helps a lot

    • @ThinkFWD
      @ThinkFWD  4 ปีที่แล้ว +1

      Uche Uba thank you l! Will do for sure.

  • @CyberGremlin110
    @CyberGremlin110 2 ปีที่แล้ว

    Thank you for this brody 🙏🏿

  • @GraceandWisdom
    @GraceandWisdom ปีที่แล้ว

    Man! I cannot thank you enough for sharing your knowledge! It seemed as though you knew the syntax in each line before you wrote the code!
    What needs to be done to create the syntax of a program with that same level of confidence in knowing what to write next?

  • @ThinkFWD
    @ThinkFWD  4 ปีที่แล้ว +2

    Please feel free to drop any comments or questions! you can be the first ! I will reply to all message! Thanks! LeetCode Solution here. According to LeetCode, this is a google question?

    • @axe-z8316
      @axe-z8316 2 ปีที่แล้ว

      why not in 4 lines ? perhaps the question is not clear ?
      function add2Number(arr1, arr2) {
      let r1 = arr1.reverse().join("");
      let r2 = arr2.reverse().join("");
      let t = parseInt(r1) + parseInt(r2);
      return("" + t).split(""); //[8,0,7]
      }

  • @sdas4451
    @sdas4451 2 ปีที่แล้ว +1

    Cannot understand how "return solution.next" is returning an array. Isn't it an object with value new Listnode(); Stuck in this part.

  • @shadizgheib4523
    @shadizgheib4523 4 ปีที่แล้ว +1

    Amazing explanation !!

    • @ThinkFWD
      @ThinkFWD  4 ปีที่แล้ว +1

      shadi zgheib thank you!

  • @orlandovaladez1733
    @orlandovaladez1733 ปีที่แล้ว

    sorry for the dumb question. I am new to leetcode and coding. what does ListNode and next stand for in this case? that's not a JS method is it? little confused. sorry I'm new.

  • @rajbannasa7662
    @rajbannasa7662 ปีที่แล้ว

    thank you so much sir

  • @discontinuity
    @discontinuity 3 ปีที่แล้ว +1

    Seems a little complicated unless I am misunderstanding.
    Why can't we just pull the numbers out into a string, convert to int, add the numbers, and then push each individual digit into a new linked list and return? And you could just push it back in reverse order as well.
    Edit: A slow, and stupid solution, but it passes 😂
    /**
    * Definition for singly-linked list.
    * function ListNode(val, next) {
    * this.val = (val===undefined ? 0 : val)
    * this.next = (next===undefined ? null : next)
    * }
    */
    /**
    * @param {ListNode} l1
    * @param {ListNode} l2
    * @return {ListNode}
    */
    var addTwoNumbers = function(l1, l2) {
    let str1 = '';
    let str2 = '';
    let tempstr = '';
    while (l1 || l2){
    if(l1){
    str1 = str1 + l1.val
    l1 = l1.next;
    }
    if(l2){
    str2 = str2 + l2.val
    l2 = l2.next;
    }
    }

    tempstr =
    BigInt(str1.split("").reverse().join("")) +
    BigInt(str2.split("").reverse().join(""));
    tempstr = tempstr.toLocaleString('fullwide', {useGrouping:false})
    let ListHead = new ListNode(0);
    let p2h = ListHead;
    for (var i = tempstr.length - 1; i >= 0; i--) {
    ListHead.val = parseInt(tempstr.charAt(i));
    if(i !== 0){
    ListHead.next = new ListNode(0);
    ListHead = ListHead.next
    }
    }
    return p2h;
    };

  • @eliotlnguyen7329
    @eliotlnguyen7329 4 ปีที่แล้ว +1

    sweeet

  • @gihdarlewa3463
    @gihdarlewa3463 ปีที่แล้ว

    Why'd you stop? You're such a good teacher T.T

  • @enkr1
    @enkr1 ปีที่แล้ว

    im practising js for interviews.
    I am away from js for quite awhile, can anyone explain to me how is `solution` being set?
    I understand the solution, but the way how it works is confusing me ...

  • @ishumishra8104
    @ishumishra8104 4 ปีที่แล้ว +2

    Hello,
    But i think we don't need pointers in linked List,could you please explain why we have used pointers also?

  • @muztosh3359
    @muztosh3359 ปีที่แล้ว

    what is ListNode ??

  • @tallgeese9715
    @tallgeese9715 3 ปีที่แล้ว

    I love you

  • @jiayang3627
    @jiayang3627 2 ปีที่แล้ว

    man this guy telling us this is easy, i couldn't figure it out lol

  • @sokpheachea
    @sokpheachea 3 ปีที่แล้ว +1

    I'm definitely don't understand 😁

  • @altayezekariyas9243
    @altayezekariyas9243 2 ปีที่แล้ว

    how about this
    var addTwoNumbers = function(l1, l2) {
    let a = l1.reverse().toString()
    let b = l2.reverse().toString()
    const sum = parseInt(a.split(",").join("") ,10) + parseInt(b.split(",").join("") ,10)
    return Array.from(String(sum), num => Number(num))
    };

    • @divine203
      @divine203 ปีที่แล้ว

      tried this method initially but, the reverse method doesn't work on leetcode