第一天 Add Two Numbers(連結串列加法)
這是leetcode第二題,對連結串列的知識基礎有一定要求。暫時自己寫不出來,先完全解析別人的程式碼。
1. 先排除極端情況,簡化後續
<span style="font-size:14px;"> if(l1 == NULL && l2) return l2;
if(l1 && l2 == NULL) return l1;
if(l1 == NULL && l2 == NULL) return NULL;</span>
2. p1,p2為了避免對原輸入的影響;head用於最後的輸出;c是進位問題;r是隨著連結串列深入下去;bUseList2是決定r沿著l1或者l2進行
由於l1,l2有完整的連結串列體系,所以沿著l1,l2的next可以避免新生成物件的麻煩。換而言之,借用現有的“變數”l1或者l2,修改成為result
<span style="font-size:14px;"> ListNode * p1 = l1; ListNode * p2 = l2; ListNode *r = l1; // r indicates the node space we will use for one digit of the result. ListNode *head = r; // head is the result list. bool bUseList2 = false; // Use this to indicate whether we should start to use l2 as resource pool. int c = 0;</span>
3. while語句表達了三種情況,即l1,l2耗盡以及沒有進位的情況(考慮:怎麼處理l1,l2同時耗盡且進位的情況);r = r->next正是借用l1,l2的巧妙之處。
<span style="font-size:14px;">while(p1 || p2 || c){ int v = c; if(p1) v += p1->val; if(p2) v += p2->val; c = v >= 10? 1: 0; r->val = v % 10; if(p1) p1 = p1->next; if(p2) p2 = p2->next; // If we haven't started to used l2, and we have reached the tail of l1, // switch l2 for the next result digit. if(bUseList2 == false && r->next == NULL){ r->next = l2; bUseList2 = true; } if(p1 || p2 || c) r = r->next; }</span>
最後把r->next用NULL堵上即可。
完整程式碼如下:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
// first take care of the empty list cases.
if(l1 == NULL && l2) return l2;
if(l1 && l2 == NULL) return l1;
if(l1 == NULL && l2 == NULL) return NULL;
ListNode * p1 = l1;
ListNode * p2 = l2;
ListNode *r = l1; // r indicates the node space we will use for one digit of the result.
ListNode *head = r; // head is the result list.
bool bUseList2 = false; // Use this to indicate whether we should start to use l2 as resource pool.
int c = 0;
while(p1 || p2 || c){
int v = c;
if(p1) v += p1->val;
if(p2) v += p2->val;
c = v >= 10? 1: 0;
r->val = v % 10;
if(p1) p1 = p1->next;
if(p2) p2 = p2->next;
// If we haven't started to used l2, and we have reached the tail of l1,
// switch l2 for the next result digit.
if(bUseList2 == false && r->next == NULL){
r->next = l2;
bUseList2 = true;
}
if(p1 || p2 || c)
r = r->next;
}
// Set the tail of the result list to NULL.
r->next = NULL;
return head;
}
相關推薦
第一天 Add Two Numbers(連結串列加法)
這是leetcode第二題,對連結串列的知識基礎有一定要求。暫時自己寫不出來,先完全解析別人的程式碼。 1. 先排除極端情況,簡化後續 <span style="font-size:14px;"> if(l1 == NULL && l2) ret
[LeetCode] Add Two Numbers(連結串列合併+模擬加法)
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and
2. Add Two Numbers(連結串列尾插法)
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their
Leetcode# 2. Add Two Numbers(連結串列模擬大數演算法)
Add Two Numbers(連結串列) You are given two non-empty linked lists representing two non-negative integers. The digits are stored in re
[leetcode] add two numbers(大資料加法連結串列版)
Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and e
leetcode2 Add Two Numbers【連結串列模擬大數加法】
給定的連結串列是倒序的 MDZZ………………我還自己寫了半天反轉連結串列,才發現不對…………然後後來又是各種報錯,發現自己寫麻煩了,總共先定義兩個指標,一個作為頭,一個往後走就可以了…………AC1.0/** * Definition for singly-linked li
LeetCode 2 — Add Two Numbers(C++ Java Python)
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes con
Add Two Numbers(兩個數相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their
2. Add Two Numbers(兩數相加)
給定兩個非空連結串列來代表兩個非負數,位數按照逆序方式儲存,它們的每個節點只儲存單個數字。將這兩數相加會返回一個新的連結串列。 你可以假設除了數字 0 之外,這兩個數字都不會以零開頭。 示例:
Add Two Numbers(兩個連結串列求和)
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain
Add Two Numbers(基於連結串列的兩數相加)
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their node
LeetCode2——Add Two Numbers(兩個連結串列中的數字相加,形成新連結串列)
鄙人不才,故收錄LeetCode中的解法和程式碼。 題目: 參考解法: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *n
LeetCode | Add Two Numbers(兩個連結串列相加)
題目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nod
leetcode鏈表--14、add-two-numbers(兩鏈表相加 得到新鏈表)
logs 錯誤 align 描述 eight val str nodes sent 題目描述 You are given two linked lists representing two non-negative numbers. The digits are sto
「LeetCode」0003-Add Two Numbers(Typescript)
分析 程式碼 /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers=function(l1, l2) { let dummyHead = new ListNod
LeetCode 2.兩數相加 Add Two Numbers (C語言)
題目描述: 給出兩個非空的連結串列用來表示兩個非負的整數。其中,它們各自的位數是按照逆序的方式儲存的,並且它們的每個節點只能儲存 一位 數字。 如果,我們將這兩個數相加起來,則會返回一個新的連結串列來表示它們的和。 您可以假設除了數字 0 之外,這兩個數都不會以 0 開頭。 示例
【leetcode日記】2.Add Two Numbers(c語言)
Description: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse ord
【LeetCode]】add Two Numbers(兩單鏈表對應數值之和)
題目 You are given two linked lists representing two non-negative numbers. The digits are stored in re
LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法規則 思路2:移花接木法。。。 問題描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法規則 根據小學學的基本加法規則。。。。。我們需要將兩個數以最低位為基準對齊,然後
Add Two Numbers (Java)
好久沒做題,邊界case沒有很注意,看來還是需要多做一做。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next;