【LeetCode-面試演算法經典-Java實現】【002-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 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
題目大意
有兩個單鏈表,代表兩個非負數,每一個節點代表一個數位,數字是反向儲存的,即第一個結點表示最低位,最後一個結點表示最高位。求兩個數的相加和,並且以連結串列形式返回。
解題思路
對兩個連結串列都從第一個開始處理,進行相加,結果再除以10求商,作為下一位相加的進位,同時記錄餘數,作為本位的結果,一直處理,直到所有的結點都處理完。
程式碼實現
/**
* 002-Add Two Numbers (單鏈表表示的兩個數相加)
* @param l1 第一個數
* @param l2 第二個數
* @return 結果
*/
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
ListNode p1 = l1;
ListNode p2 = l2;
ListNode root = new ListNode(0); // 頭結點
ListNode r = root;
root.next = l1;
int carry = 0; // 初始進位
int sum;
while (p1 != null && p2 != null) {
sum = p1.val + p2.val + carry;
p1.val = sum % 10; // 本位的結果
carry = sum / 10; // 本次進位
r.next = p1;
r = p1; // 指向最後一個相加的結點
p1 = p1.next;
p2 = p2.next;
}
if (p1 == null) {
r.next = p2;
} else {
r.next = p1;
}
// 最後一次相加還有進位
if (carry == 1) {
// 開始時r.next是第一個要相加的結點
while (r.next != null) {
sum = r.next.val + carry;
r.next.val = sum % 10;
carry = sum / 10;
r = r.next;
}
// 都加完了還有進位,就要建立一個新的結點
if (carry == 1) {
r.next = new ListNode(1);
}
}
return root.next;
}
}
評測結果
點選圖片,滑鼠不釋放,拖動一段位置,釋放後在新的視窗中檢視完整圖片。
特別說明
相關推薦
【LeetCode-面試演算法經典-Java實現】【067-Add Binary(二進位制加法)】
原題 Given two binary strings, return their sum (also a binary string). For example, a
【LeetCode-面試演算法經典-Java實現】【151-Reverse Words in a String(反轉字串中的單詞)】
原題 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "bl
【LeetCode-面試演算法經典-Java實現】【136-Single Number(只出現一次的數字)】
原題 Given an array of integers, every element appears twice except for one. Find that singl
【LeetCode-面試演算法經典-Java實現】【153-Find Minimum in Rotated Sorted Array(找旋轉陣列中的最小數字)】
原題 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0
【LeetCode-面試演算法經典-Java實現】【021-Merge Two Sorted Lists(合併兩個排好序的單鏈表)】
原題 Merge two sorted linked lists and return it as a new list. The new list should be made
【LeetCode-面試演算法經典-Java實現】【019-Remove Nth Node From End of List(移除單鏈表的倒數第N個節點)】
原題 Given a linked list, remove the nth node from the end of list and return its head. F
【LeetCode-面試演算法經典-Java實現】【003-Longest Substring Without Repeating Characters(最長非重複子字串)】
原題 Given a string, find the length of the longest substring without repeating characters. For
【LeetCode-面試演算法經典-Java實現】【064-Minimum Path Sum(最小路徑和)】
原題 Given a m x n grid filled with non-negative numbers, find a path from top left to botto
【LeetCode-面試演算法經典-Java實現】【198-House Robber(搶劫犯)】
原題 You are a professional robber planning to rob houses along a street. Each house h
【LeetCode-面試演算法經典-Java實現】【142-Linked List Cycle II(單鏈表中有環II)】
原題 Given a linked list, return the node where the cycle begins. If there is no cycle, retu
【LeetCode-面試演算法經典-Java實現】【134-Gas Station(加油站問題)】
原題 There are N gas stations along a circular route, where the amount of gas at station i i
【LeetCode-面試演算法經典-Java實現】【110-Balanced Binary Tree(平衡二叉樹)】
原題 Given a binary tree, determine if it is height-balanced. For this problem, a height-
【LeetCode-面試演算法經典-Java實現】【154-Find Minimum in Rotated Sorted Array II(找旋轉陣列中的最小數字II)】
原題 Follow up for “Find Minimum in Rotated Sorted Array”: What if duplicates are allow
【LeetCode-面試演算法經典-Java實現】【206-Reverse Linked List(反轉一個單鏈表)】
原題 Reverse a singly linked list. 題目大意 反轉單鏈表。 解題思路 使用頭插法。 程式碼實現
【LeetCode-面試演算法經典-Java實現】【006-ZigZag Conversion(Z字型轉換)】
原題 The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows
【LeetCode-面試演算法經典-Java實現】【073-Climbing Stairs(爬樓梯)】
原題 You are climbing a stair case. It takes n steps to reach to the top. Each time you c
【LeetCode-面試演算法經典-Java實現】【165-Compare Version Numbers(比較版本號)】
原題 Compare two version numbers version1 and version2. If version1 > version2 return
【LeetCode-面試演算法經典-Java實現】【002-Add Two Numbers (單鏈表表示的兩個數相加)】
原題 You are given two linked lists representing two non-negative numbers. The digits are stor
【LeetCode-面試演算法經典-Java實現】【011-ContainerWithMostWater(容納最多的水)】
原題 Given n non-negative integers a1, a2, …, an, where each represents a point at coordin
【LeetCode-面試演算法經典-Java實現】【017-Letter Combinations of a Phone Number (電話號碼上的單詞組合)】
原題 Given a digit string, return all possible letter combinations that the number could rep