Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3218165

Browse files
committed
Add Two Numbers
1 parent 2809814 commit 3218165

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

‎Add Two Numbers.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*题目:
2+
3+
链接:https://leetcode.com/problems/add-two-numbers/description/
4+
5+
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 a single digit. Add the two numbers and return it as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
10+
Output: 7 -> 0 -> 8
11+
12+
*/
13+
14+
/*
15+
解析:
16+
作者知乎链接:https://www.zhihu.com/people/bing-mo-43-95/activities
17+
18+
解题思路:先把他当作两个普通的链表,进行相加,一个一个对应相加,很好实现。
19+
而在这题的难度是存在特殊的意义,即代表着两个数字的相加,这样的话就存在一个进位的问题。
20+
可以使用一个变量来存储进位,再加到下一位。
21+
22+
*/
23+
24+
25+
/**
26+
* Definition for singly-linked list.
27+
* public class ListNode {
28+
* int val;
29+
* ListNode next;
30+
* ListNode(int x) { val = x; }
31+
* }
32+
*/
33+
class Solution {
34+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
35+
ListNode result = new ListNode(0);
36+
ListNode head1,head2,head3;//两个临时变量
37+
head1=l1;
38+
head2=l2;
39+
head3=result;
40+
//进位值digit
41+
int digit=0;
42+
while(head1!=null&&head2!=null) {
43+
ListNode LNode = new ListNode(head1.val+head2.val+digit);
44+
45+
//如果相加结果大于等于10(即出现进位),则将val取10的余数。并将进位值设成1
46+
if(LNode.val>=10){
47+
LNode.val=LNode.val%10;
48+
digit = 1;
49+
}
50+
51+
else{
52+
//否则将数位归零
53+
digit=0;
54+
}
55+
56+
head3.next = LNode;
57+
head1=head1.next;
58+
head2=head2.next;
59+
head3=head3.next;
60+
61+
62+
63+
64+
}
65+
//解决两个数位不想等的情况
66+
while(head1!=null){
67+
ListNode LNode = new ListNode(head1.val+digit);
68+
if(LNode.val>=10){
69+
LNode.val=LNode.val%10;
70+
digit = 1;
71+
}
72+
else{
73+
digit=0;//将数位归零
74+
}
75+
head3.next=LNode;
76+
head3=head3.next;
77+
head1=head1.next;
78+
79+
}
80+
81+
while(head2!=null){
82+
ListNode LNode = new ListNode(head2.val+digit);
83+
if(LNode.val>=10){
84+
LNode.val=LNode.val%10;
85+
digit = 1;
86+
}
87+
else{
88+
digit=0;//将数位归零
89+
}
90+
head3.next=LNode;
91+
head3=head3.next;
92+
head2=head2.next;
93+
}
94+
95+
//最后一种情况,进位值仍然为1的时候
96+
if(digit==1){
97+
ListNode LNode = new ListNode(1);
98+
head3.next=LNode;
99+
}
100+
101+
return result.next;
102+
}
103+
}

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@
3232
# 目录
3333

3434
1. two sum :https://github.com/ZTYZZ/leetcode/blob/master/Two%20Sum.java
35+
2. Add Two Numbers: https://leetcode.com/problems/add-two-numbers/description/
36+
3537

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /