Skip to content

Commit 62af305

Browse files
committed
Update Contents
1 parent 44db1c1 commit 62af305

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
1818
|203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/#/description) | [Python](./linkedlist/RemoveLinkedListElements.py) | _O(n)_| _O(1)_ | Easy || |
1919
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/#/description) | [Python](./linkedlist/ReverseLinkedList.py) | _O(n)_| _O(1)_ | Easy |CC189| [Video Tutorial](https://www.youtube.com/watch?v=sYcOK51hl-A&t=681s) |
2020
|234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) | [Python](./linkedlist/palindrome.py) | _O(n)_| _O(1)_ | Easy |CC189| Two Pointers|
21+
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/#/description) | [Python](./linkedlist/addTwoNumbers.py) | _O(n)_| _O(n)_ | Medium |CC189| ||

linkedlist/addTwoNumbers.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Author: Yu Zhou
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+
# You may assume the two numbers do not contain any leading zero, except the number 0 itself.
7+
# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
8+
# Output: 7 -> 0 -> 8
9+
10+
# ****************
11+
# Final Solution *
12+
# ****************
13+
class Solution(object):
14+
def addTwoNumbers(self, n1, n2):
15+
"""
16+
:type l1: ListNode
17+
:type l2: ListNode
18+
:rtype: ListNode
19+
"""
20+
ret = ListNode(0)
21+
temp = ret
22+
carry = 0
23+
while n1 or n2 or carry:
24+
result = carry
25+
if n1:
26+
result += n1.val
27+
n1 = n1.next
28+
if n2:
29+
result += n2.val
30+
n2 = n2.next
31+
newNode = ListNode(result % 10)
32+
carry = result / 10
33+
temp.next = newNode
34+
temp = temp.next
35+
return ret.next
36+
37+
38+
# ***************************************
39+
# The following code is an fail attempt *
40+
# ***************************************
41+
42+
class Solution(object):
43+
def addTwoNumbers(self, n1, n2):
44+
ret = ListNode(0)
45+
temp = ret
46+
carry = 0
47+
while n1 or n2:
48+
result = carry
49+
50+
if n1:
51+
result += n1.val
52+
n1 = n1.next
53+
if n2:
54+
result += n2.val
55+
n2 = n2.next
56+
newNode = ListNode(result % 10)
57+
carry = result / 10
58+
print newNode.val
59+
temp.next = newNode
60+
return ret.next
61+
62+
# 代码解析:
63+
# 首先代码打印不出来,因为忘记了一个重要的步骤
64+
# 当把Temp.next指向newNode以后,我没有更新Temp本身,所以,temp永远处于
65+
# ret的位置,所以应该是
66+
# temp.next = newNode
67+
# temp = temp.next
68+
69+
# 然后代码还是不工作,因为忘记了一个Edge case
70+
# 当这段代码运行至 n1或者n2的底端,自动就结束了
71+
# 若是最后两个数相加有一个carry,就会被Truncate掉
72+
# 所以应该在while里面加一个condition
73+
# while n1 or n2 or carry:

0 commit comments

Comments
 (0)