Skip to content

Commit d7a104c

Browse files
committed
This problem took me 3 hours lol
1 parent 62af305 commit d7a104c

File tree

3 files changed

+230
-1
lines changed

3 files changed

+230
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ 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| ||
21+
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/#/description) | [Python](./linkedlist/addTwoNumbers.py) | _O(n)_| _O(n)_ | Medium |CC189| |
22+
|445| [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/#/description)| [Python](./linkedlist/addTwoNumbersTwo.py) | _O(n)_| _O(n)_ | Medium |CC189 |Stack|

linkedlist/addTwoNumbersTwo.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
6+
# Example:
7+
# Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
8+
# Output: 7 -> 8 -> 0 -> 7
9+
10+
# ****************
11+
# Final Solution *
12+
# Using Stack *
13+
# ****************
14+
# Definition for singly-linked list.
15+
# class ListNode(object):
16+
# def __init__(self, x):
17+
# self.val = x
18+
# self.next = None
19+
20+
class Solution(object):
21+
def addTwoNumbers(self, n1, n2):
22+
l1 = self.list_to_stack(n1)
23+
l2 = self.list_to_stack(n2)
24+
stack = self.add_helper(l1,l2)
25+
return self.stack_to_list(stack)
26+
27+
def list_to_stack(self, node):
28+
stack = []
29+
while node:
30+
stack.append(node.val)
31+
node = node.next
32+
return stack
33+
34+
def add_helper(self, s1, s2):
35+
res = []
36+
carry = 0
37+
while s1 or s2 or carry:
38+
num1 = s1.pop() if s1 else 0
39+
num2 = s2.pop() if s2 else 0
40+
total = num1 + num2 + carry
41+
res.append(total % 10)
42+
carry = total/10
43+
return res
44+
45+
def stack_to_list(self, stack):
46+
head = ListNode(0)
47+
cur = head
48+
while stack:
49+
cur.next = ListNode(stack.pop())
50+
cur = cur.next
51+
return head.next
52+
53+
54+
# *****************************************
55+
# The following code is an fail attempt *
56+
# Passed 893/1539 Test Cases *
57+
# Failed Cases such as 1 + 99(Getting 01) *
58+
# *****************************************
59+
class Solution(object):
60+
def addTwoNumbers(self, n1, n2):
61+
#Pad The Two List
62+
len1, len2 = self.get_length(n1), self.get_length(n2)
63+
if len1 > len2:
64+
#Insert Dummy Node to head
65+
for _ in range(len1-len2):
66+
temp = ListNode(0)
67+
temp.next = n2
68+
n2 = temp
69+
else:
70+
for _ in range(len2-len1):
71+
temp = ListNode(0)
72+
temp.next = n1
73+
n1 = temp
74+
75+
has_carry_head = False
76+
#Edge: Largest Carry
77+
res = cur = ListNode(0)
78+
guard1, guard2 = n1, n2
79+
carry = 0
80+
while n1 or n2 or carry:
81+
if n1.next and n2.next:
82+
carry = (n1.next.val + n2.next.val) / 10
83+
result = carry + n1.val + n2.val
84+
temp = ListNode(result % 10)
85+
front_carry = result / 10
86+
cur.next = temp
87+
cur = cur.next
88+
89+
n1 = n1.next
90+
n2 = n2.next
91+
92+
if guard1.val + guard2.val + front_carry > 10:
93+
res.val = 1
94+
has_carry_head = True
95+
96+
print has_carry_head
97+
return res if has_carry_head else res.next
98+
99+
def display(self,node):
100+
cur = node
101+
arr = []
102+
while cur:
103+
arr.append(cur.val)
104+
cur = cur.next
105+
print arr
106+
107+
def get_length(self, node):
108+
cur = node
109+
count = 0
110+
while cur:
111+
count += 1
112+
cur = cur.next
113+
return count
114+
115+
#失败心经
116+
# 讲道理,现在还是懵的。。首先insert方程和pad方程
117+
# 若是单独写一个方程,然后带入,不知道为什么不能pass by reference
118+
# 所以后来很机械化的没有写进方程
119+
# 这个代码大部分的Test Case都能过,有一些小的,是在不知道怎么调了
120+
# 因为弄得特别的乱,大概思路就是Pad两个List,然后加减,CC189的套路
121+
# 这道题后来看了答案,可以用Stack来写。

linkedlist/ll.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
#=======================================================================
4+
# Author: Yu Zhou
5+
# Singly Linked List
6+
#=======================================================================
7+
from random import randint
8+
9+
class Node(object):
10+
#Constructor
11+
def __init__(self, val, next = None):
12+
self.val = val
13+
self.next = next
14+
15+
class LinkedList(object):
16+
#Constructor
17+
def __init__(self, head = None, size = None):
18+
self.head = head
19+
self.size = size
20+
21+
#Display
22+
def __str__(self):
23+
res = []
24+
cur = self.head
25+
while cur:
26+
res.append(str(cur.val))
27+
cur = cur.next
28+
return ' -> '.join(res)
29+
30+
31+
32+
#在头Insert
33+
def insert_head(self, val):
34+
if self.head == None:
35+
self.head = Node(val)
36+
else:
37+
temp = Node(val) #创建Temp Node
38+
temp.next = self.head #指向Head
39+
self.head = temp #把head的reference改到这个Temp Node
40+
41+
#在末尾Insert
42+
def insert_tail(self, val):
43+
if self.head == None:
44+
self.head = Node(val)
45+
else:
46+
cur = self.head
47+
temp = Node(val)
48+
while cur.next: #位移
49+
cur = cur.next
50+
cur.next = temp
51+
52+
#Generate Insert 10位数
53+
def generate(self):
54+
for _ in xrange(10):
55+
self.insert_tail(randint(0,9))
56+
57+
#Insert自定义多位数
58+
def insert_multiple(self, val):
59+
for v in val:
60+
self.insert_tail(v)
61+
62+
#Search
63+
def search(self, val):
64+
if self.head == None:
65+
raise ValueError("List is empty")
66+
67+
cur = self.head
68+
while cur:
69+
if cur.val == val:
70+
return True
71+
else:
72+
cur = cur.next
73+
return False
74+
75+
#Delete V1 using Prev
76+
def delete_1(self, val):
77+
if self.head == None:
78+
return self.head
79+
80+
cur = self.head
81+
prev = None
82+
83+
while cur:
84+
if cur.val == val:
85+
if prev:
86+
prev.next = cur.next
87+
else:
88+
self.head = cur.next
89+
prev = cur
90+
cur = cur.next
91+
92+
#Delete V2 not using Prev
93+
def delete_2(self, val):
94+
if self.head == None:
95+
return self.head
96+
97+
cur = self.head
98+
#Edge case
99+
if cur.val == val:
100+
self.head = cur.next
101+
102+
#The rest
103+
while cur.next:
104+
if cur.next.val == val:
105+
cur.next = cur.next.next
106+
else:
107+
cur = cur.next

0 commit comments

Comments
 (0)