摘要:問題描述先來看看原題描述為方便,使用作爲實現(xiàn)之程式語言。初始代碼如下觀察到如下幾點題目定義了加法順序,從左至右?guī)нM位算使用預定義的單鏈表數(shù)據(jù)結(jié)構(gòu)結(jié)果返回一個類型,應該是鏈表頭。思考過程思路如圖所示實現(xiàn)實現(xiàn)
問題描述
先來看看原題描述:
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 作爲實現(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) { // Your implementations here } }
觀察到如下幾點:
題目定義了加法順序,從左至右?guī)нM位運算;
使用預定義的單鏈表數(shù)據(jù)結(jié)構(gòu);
結(jié)果返回一個 ListNode 類型,應該是鏈表頭。
思考過程思路如圖所示:
Iterative 實現(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 實現(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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/66093.html
摘要:這題是說給出兩個鏈表每個鏈表代表一個多位整數(shù)個位在前比如代表著求這兩個鏈表代表的整數(shù)之和同樣以倒序的鏈表表示難度這個題目就是模擬人手算加法的過程需要記錄進位每次把對應位置兩個節(jié)點如果一個走到頭了就只算其中一個的值加上進位值 Add Two Numbers You are given two linked lists representing two non-negative num...
摘要:描述中文解釋給定兩個非空的鏈表里面分別包含不等數(shù)量的正整數(shù),每一個節(jié)點都包含一個正整數(shù),肯能是,但是不會是這種情況。我們需要按照倒序計算他們的和然后再次倒序輸出。 描述 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in rev...
摘要:我們的目的是求出兩個數(shù)字的加和,并以同樣的形式返回。假設(shè)每個都不會存在在首位的,除非數(shù)字本身就是想法這道題主要要求還是熟悉的操作。這道題由于數(shù)字反序,所以實際上從首位開始相加正好符合我們筆算的時候的順序。 題目詳情 You are given two non-empty linked lists representing two non-negative integers. The d...
摘要:題目要求對以鏈表形式的兩個整數(shù)進行累加計算。思路一鏈表轉(zhuǎn)置鏈表形式跟非鏈表形式的最大區(qū)別在于我們無法根據(jù)下標來訪問對應下標的元素。因此這里通過先將鏈表轉(zhuǎn)置,再從左往右對每一位求和來進行累加。通過棧可以實現(xià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 ...
閱讀 3309·2021-11-18 10:02
閱讀 2755·2019-08-30 13:56
閱讀 416·2019-08-29 12:36
閱讀 527·2019-08-28 18:07
閱讀 716·2019-08-27 10:51
閱讀 3453·2019-08-26 12:13
閱讀 3291·2019-08-26 11:46
閱讀 3318·2019-08-23 12:00