[容易]167.鏈表求和

我是小小強,這是我的第8篇原創(chuàng)文章,閱讀需要大約10分鐘。


題目

LintCode:鏈表求和

描述

你有兩個用鏈表代表的整數(shù),其中每個節(jié)點包含一個數(shù)字。數(shù)字存儲按照在原來整數(shù)中相反的順序,使得第一個數(shù)字位于鏈表的開頭。寫出一個函數(shù)將兩個整數(shù)相加,用鏈表形式返回和。

樣例

給出兩個鏈表 3->1->5->null5->9->2->null,返回8->0->8->null

思路

題目中要求從鏈表頭節(jié)點開始,按位相加,同時進位。那就可以構(gòu)造一個鏈表,存放相加結(jié)果,同時用一個臨時變量存放鏈表相加之和,如果大于10,則設(shè)置進位標志。最后結(jié)束時判斷進位標志是否為0,不為0,則需要再進位。

實現(xiàn)

  1. java實現(xiàn)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;      
 *     }
 * }
 */
public class Solution {
    /**
     * @param l1: the first list
     * @param l2: the second list
     * @return: the sum list of l1 and l2 
     */
    public ListNode addLists(ListNode l1, ListNode l2) {
        // write your code here
        int flag = 0;
        int a = 0;
        ListNode ln = new ListNode(0);
        ListNode tmpln = ln, tmpl1 = l1, tmpl2 = l2, tmp = null;
        if (l1 == null){
            return l2;
        }
        if (l2 == null){
            return l1;
        }
        if (l1 == null && l2 == null) {
            return null;
        }
        while (tmpl1 != null || tmpl2 != null){
            if (tmpl1 != null){
                a += tmpl1.val;
            }
            if (tmpl2 != null){
                a += tmpl2.val;
            }
            if (flag > 0) {
                a += flag;
            }
            if( a>=10 ) {
                flag = 1;
                a -= 10;
            }
            else
                flag = 0;
            tmp = new ListNode(a);
            tmpln.next = tmp;
            tmpln = tmp;
            tmpln.next = null;
            a = 0;
            if (tmpl1 != null)
                tmpl1 = tmpl1.next;
            if (tmpl2 != null)
                tmpl2 = tmpl2.next;
        }
        if (flag > 0){
            tmp = new ListNode(1);
            tmpln.next = tmp;
        }
        return ln.next;
    }
}
  1. 結(jié)果分析
    結(jié)果:結(jié)果通過了LintCode的要求。
    分析:這種實現(xiàn)比較好理解,但是實現(xiàn)起來卻沒什么巧妙之處。

其它優(yōu)化參考

LeetCode discuss

優(yōu)秀代碼

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode c1 = l1;
        ListNode c2 = l2;
        ListNode sentinel = new ListNode(0);
        ListNode d = sentinel;
        int sum = 0;
        while (c1 != null || c2 != null) {
            sum /= 10;
            if (c1 != null) {
                sum += c1.val;
                c1 = c1.next;
            }
            if (c2 != null) {
                sum += c2.val;
                c2 = c2.next;
            }
            d.next = new ListNode(sum % 10);
            d = d.next;
        }
        if (sum / 10 == 1)
            d.next = new ListNode(1);
        return sentinel.next;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容