摘要:?jiǎn)栴}描述先來(lái)看看原題描述為方便,使用作爲(wèi)實(shí)現(xiàn)之程式語(yǔ)言。初始代碼如下觀察到如下幾點(diǎn)題目定義了加法順序,從左至右?guī)нM(jìn)位算使用預(yù)定義的單鏈表數(shù)據(jù)結(jié)構(gòu)結(jié)果返回一個(gè)類(lèi)型,應(yīng)該是鏈表頭。思考過(guò)程思路如圖所示實(shí)現(xiàn)實(shí)現(xiàn)
問(wèn)題描述
先來(lái)看看原題描述:
You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
為方便,使用 Java 作爲(wèi)實(shí)現(xiàn)之程式語(yǔ)言。
初始代碼如下:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { // Your implementations here } }
觀察到如下幾點(diǎn):
題目定義了加法順序,從左至右?guī)нM(jìn)位運(yùn)算;
使用預(yù)定義的單鏈表數(shù)據(jù)結(jié)構(gòu);
結(jié)果返回一個(gè) ListNode 類(lèi)型,應(yīng)該是鏈表頭。
思考過(guò)程思路如圖所示:
Iterative 實(shí)現(xiàn)/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { /* Implementation begins */ int carry = 0; int sum = 0; ListNode result = new ListNode(0); // keep node list ListNode current = result; // current pointer do { sum = (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0) + carry; current.next = new ListNode(sum % 10); carry = sum / 10; l1 = l1 != null ? l1.next : null; l2 = l2 != null ? l2.next : null; current = current.next; } while (l1 != null || l2 != null); if (carry != 0) current.next = new ListNode(carry); return result.next; /* Implementation ends */ } }Recursive 實(shí)現(xiàn)
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); return addTwoNumbers(l1, l2, 0, dummy, dummy); } private ListNode addTwoNumbers(ListNode l1, ListNode l2, int carry, ListNode current, ListNode result) { if (l1 == null && l2 == null) { if (carry != 0) current.next = new ListNode(carry); return result.next; } else { l1 = (l1 == null) ? new ListNode(0) : l1; l2 = (l2 == null) ? new ListNode(0) : l2; } int sum = l1.val + l2.val + carry; carry = sum / 10; current.next = new ListNode(sum % 10); return addTwoNumbers(l1.next, l2.next, carry, current.next, result); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/66093.html
摘要:這題是說(shuō)給出兩個(gè)鏈表每個(gè)鏈表代表一個(gè)多位整數(shù)個(gè)位在前比如代表著求這兩個(gè)鏈表代表的整數(shù)之和同樣以倒序的鏈表表示難度這個(gè)題目就是模擬人手算加法的過(guò)程需要記錄進(jìn)位每次把對(duì)應(yīng)位置兩個(gè)節(jié)點(diǎn)如果一個(gè)走到頭了就只算其中一個(gè)的值加上進(jìn)位值 Add Two Numbers You are given two linked lists representing two non-negative num...
摘要:描述中文解釋給定兩個(gè)非空的鏈表里面分別包含不等數(shù)量的正整數(shù),每一個(gè)節(jié)點(diǎn)都包含一個(gè)正整數(shù),肯能是,但是不會(huì)是這種情況。我們需要按照倒序計(jì)算他們的和然后再次倒序輸出。 描述 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in rev...
摘要:我們的目的是求出兩個(gè)數(shù)字的加和,并以同樣的形式返回。假設(shè)每個(gè)都不會(huì)存在在首位的,除非數(shù)字本身就是想法這道題主要要求還是熟悉的操作。這道題由于數(shù)字反序,所以實(shí)際上從首位開(kāi)始相加正好符合我們筆算的時(shí)候的順序。 題目詳情 You are given two non-empty linked lists representing two non-negative integers. The d...
摘要:題目要求對(duì)以鏈表形式的兩個(gè)整數(shù)進(jìn)行累加計(jì)算。思路一鏈表轉(zhuǎn)置鏈表形式跟非鏈表形式的最大區(qū)別在于我們無(wú)法根據(jù)下標(biāo)來(lái)訪問(wèn)對(duì)應(yīng)下標(biāo)的元素。因此這里通過(guò)先將鏈表轉(zhuǎn)置,再?gòu)淖笸覍?duì)每一位求和來(lái)進(jìn)行累加。通過(guò)棧可以實(shí)現(xiàn)先進(jìn)后出,即讀取順序的轉(zhuǎn)置。 題目要求 You are given two non-empty linked lists representing two non-negative i...
Problem 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 ...
閱讀 3311·2021-11-18 10:02
閱讀 2757·2019-08-30 13:56
閱讀 419·2019-08-29 12:36
閱讀 530·2019-08-28 18:07
閱讀 720·2019-08-27 10:51
閱讀 3455·2019-08-26 12:13
閱讀 3295·2019-08-26 11:46
閱讀 3321·2019-08-23 12:00