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(Easy)
  • 2.Code

Was this helpful?

  1. Phone Interview II

167.Add Two Numbers

Previous31.Partition ArrayNext88.Lowest Common Ancestor

Last updated 5 years ago

Was this helpful?

1.Description(Easy)

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored inreverseorder, 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

Given7->1->6 + 5->9->2. That is,617 + 295.

Return2->1->9. That is912.

Given3->1->5and5->9->2, return8->0->8.

2.Code

从l1, l2头节点开始,对应位置相加并建立新节点。用一个变量carry记录进位。注意几种特殊情况:

1.一个链表为空

l1: NULL

l2: 1->2

sum: 1->2

2.l1, l2长度不同,且结果有可能长度超过l1, l2中的最大长度

l1: 2->2

l2: 9->9->9

sum: 1->2->0->1

public ListNode addLists(ListNode l1, ListNode l2) {

        if(l1==null && l2==null){
            return null;
        }

        ListNode dummy=new ListNode(0);
        ListNode current=dummy;
        int carry=0; //控制进位
        while(l1!=null && l2!=null){
            int sum=l1.val+l2.val+carry;//注意每次都要加上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=carry/10;
            current.next=new ListNode(sum%10);

            l2=l2.next;
            current=current.next;
        }

        //最后判断下是否还有进位
        if(carry!=0){
            current.next=new ListNode(carry);
        }
        return dummy.next;
    }
Tags
Cracking The Coding Interview
Linked List
High Precision