Amazon
  • Introduction
  • Phone Interview I
    • 53.Reverse Words in a String
    • 31.Partition Array
  • Phone Interview II
    • 167.Add Two Numbers
    • 88.Lowest Common Ancestor
  • Onsite I
    • 655.Big Integer Addition
    • 221.Add Two Numbers II
  • Onsite II
    • 158.Two Strings Are Anagrams
    • 171.Anagrams
    • 386.Longest Substring with At Most K Distinct Characters
  • Onsite III
    • 479.Second Max of Array
    • 589.Connecting Graph
  • Onsite IV
    • 532.Reverse Pairs
  • 2022
    • OA
      • work simulation
      • Greyness
      • NearestRetailer
      • Sum of Scores of Subarray
      • StrengthOfPassword
      • ProductOf1
      • Move 0/1 InArray
      • Max deviation among all substrings
      • AWS power consumption
      • searchWordResultWord
      • maxOperationOfString
      • MinHealthGame
      • EarliestMonth
      • Package ship
      • RatingConsectiveDecresing
      • LinkedListSum
      • MovingBoxes
      • ValidString
      • MaxValueAfterRemovingFromString
      • Subtree with Maximum Average
    • VO
      • 2022.3
    • BQ
      • doc
      • 2022.4
      • Freq Question
      • 11大类BQ和Follow-ups
      • Page 1
      • BQ 100
      • 35 behavioral questions asked in 95% of Amazon interviews with examples
      • Page 2
      • 反向BQ
    • LP
      • LP-1
      • LP-2
    • SD
      • Design Amazon Prime Video Recommendation System
      • Amazon Order system
    • OOD
      • Linux Find Command
      • Amazon Locker
    • AWS Identity call
    • Interviews
Powered by GitBook
On this page
  • 1.Description(Medium)
  • 2.Code

Was this helpful?

  1. Onsite I

221.Add Two Numbers II

Previous655.Big Integer AdditionNext158.Two Strings Are Anagrams

Last updated 5 years ago

Was this helpful?

1.Description(Medium)

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored inforwardorder, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

Example

Given6->1->7 + 2->9->5. That is,617 + 295.

Return9->1->2. That is,912.

2.Code

跟167 add two numbers 一样,只不过加了reverse基本操作

 public ListNode addLists2(ListNode l1, ListNode l2) {
        if(l1==null && l2==null){
            return null;
        }

        ListNode dummy=new ListNode(0);
        ListNode current=dummy;
        int carry=0;
        l1=reverse(l1);
        l2=reverse(l2);
        while(l1!=null && l2!=null){
            int sum=l1.val+l2.val+carry;
            carry=sum/10;
            current.next=new ListNode(sum%10);

            l1=l1.next;
            l2=l2.next;
            current=current.next;
        }
        while(l1!=null){
            int sum=l1.val+carry;
            carry=sum/10;
            current.next=new ListNode(sum%10);

            l1=l1.next;
            current=current.next;
        }
        while(l2!=null){
            int sum=l2.val+carry;
            carry=sum/10;
            current.next=new ListNode(sum%10);

            l2=l2.next;
            current=current.next;
        }
        if(carry!=0){
            current.next=new ListNode(carry);
        }
        return reverse(dummy.next);
    } 

    public ListNode reverse(ListNode head){
        ListNode prev=null;
        while(head!=null){
            ListNode temp=head.next;
            head.next=prev;
            prev=head;
            head=temp;
        }
        return prev;
    }
Tags
Linked List
High Precision