445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

給定兩個鏈表,其中每個鏈表都代表一個數字,如:(7 -> 2 -> 4 -> 3) =7243,輸出將兩個鏈表相加的結果,以鏈表方式表示(但不能倒反節點進行操作)

題解

如下例:

 

元素

鏈表1

1->2->3->4

鏈表2

9->9->9->9->9

  1. 歷遍兩個鏈表,得出兩個鏈表的長度

 

元素

長度

鏈表1

9->9->9->9->9

5

鏈表2

1->2->3->4

4

  1. 比較兩個鏈表的長度,若鏈表2的長度大於鏈表1,則交換彼此

 

元素

長度

鏈表1

9->9->9->9->9

5

鏈表2

1->2->3->4

4

  1. 將鏈表1的指針指到鏈表2頭指針的首位數的位置

9->*9->9->9->9

   *1->2->3->4                   *指針

  1. 對鏈表1與2進行每一位數的相加(相加在表1上)

9->10->11->12->13

        1->2->3->4

  1. 對鏈表1除了第一個節點(第1位數),其他位數節點進行進位操作(反覆檢查至每一位都有進行進位)

10->1->2->3->3

  1. 檢查第一位是否大於9,有則進行進位操作(在第一個節點前面加一個節點),若不用進行進位操作則返回第1個節點

1->0->1->2->3->3

  1. 若第1位進行進位操作,則返回最新添加的節點

 

package LeetCode.Medium;

import LeetCode.Dependencies.ListNode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class AddTwoNumbersII {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result = null;
        if(l1 == null || l2 == null)
            return result;
        
        //得到兩個鏈表的長度
        int l1len = getLength(l1);
        int l2len = getLength(l2);
        
        //若l2長度大於l1的長度,就交換它們
        if(l2len > l1len) {
            ListNode tmp = l2;
            l2 = l1;
            l1 = tmp;
            
            int lenTmp = l2len;
            l2len = l1len;
            l1len = lenTmp;
        }
        
        //設置l1的頭指針
        ListNode l1head = l1;
        
        //將l1的指針與l2的頭指針進行對齊,以方便相加
        int moveStep = l1len - l2len;
        for(int i = 0; i < moveStep; i ++) {
            l1 = l1.next;
        }
        
        //將l1與l2相加
        while(l1 != null && l2 != null) {
            l1.val += l2.val;
            l1 = l1.next;
            l2 = l2.next;
        }
        
        //處理第一位之後的進位,並檢查每一位都有確實進位
        boolean needCheck = true;
        while(needCheck == true) {
            l1 = l1head;
            needCheck = false;
            while(l1 != null) {
                if(l1.next != null) {
                    if(l1.next.val > 9) {
                        l1.next.val = l1.next.val % 10;
                        l1.val += 1;
                        needCheck = true;
                    }
                }
                l1 = l1.next;
            }
        }
        
        //處理第一位進位,若第一位需要進位則在前面多一個節點
        l1 = l1head;
        if(l1.val > 9) {
            ListNode head = new ListNode(1);
            l1.val = l1.val % 10;
            head.next = l1;
            
            return head;
        }
            
        return l1;
    }
    
    
    int getLength(ListNode list) {
        int length = 0;
        while(list != null) {
            length ++;
            list = list.next;
        }
        
        return length;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值