8000 <feat> add problem 21 Merge Two Sorted Lists · EvolutionMax/leetcode_python@1ceefe9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ceefe9

Browse files
committed
<feat> add problem 21 Merge Two Sorted Lists
Author:rapospectre@gmail.com Desc: add problem 21 Merge Two Sorted Lists
1 parent ead36bc commit 1ceefe9

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#### 18. [4Sum][37]: [Solution][38]
3434
#### 19. [Remove Nth Node From End of List][39]: [Solution][40]
3535
#### 20. [Valid Parentheses][41]: [Solution][42]
36+
#### 21. [Merge Two Sorted Lists][43]: [Solution][44]
3637

3738
[1]: https://leetcode.com/problems/two-sum/
3839
[2]: https://github.com/bluedazzle/leetcode_python/blob/master/src/two_sum.py
@@ -75,4 +76,6 @@
7576
[39]: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
7677
[40]: https://github.com/bluedazzle/leetcode_python/blob/master/src/remove_nth_node_from_end_of_list.py
7778
[41]: https://leetcode.com/problems/valid-parentheses/
78-
[42]: https://github.com/bluedazzle/leetcode_python/blob/master/src/valid_parentheses.py
79+
[42]: https://github.com/bluedazzle/leetcode_python/blob/master/src/valid_parentheses.py
80+
[43]: https://leetcode.com/problems/merge-two-sorted-lists/
81+
[44]: https://github.com/bluedazzle/leetcode_python/blob/master/src/merge_two_sorted_lists.py

src/merge_two_sorted_lists.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# coding: utf-8
2+
# ## ##
3+
# ##### # ####
4+
# ######### ## #######
5+
# ### ############ ##
6+
# #### # ####### #########
7+
# ########### ######### # ########### ##
8+
# ############# ### #### ## #### ######## ##
9+
# ## ## ## ## ### ######### ######### ### ## # ### ## #
10+
# ## ## ## ### ######## ########## #### ## ### #### ### ##### ####
11+
# ## #### ## ## ######### ########## ######## ##### #### #### ##### #####
12+
# ###### #### ## ## ### ## ### ############ ## ### ## ## ## ### ### ##### ### ##
13+
# #### ##### ## ### ### ###### ############ ## ## ##### ### ## ### ### #####
14+
# ## ## ### ### ### ### ######### ############## ### ## #### ## ### ##### #### ####
15+
# ## ### ## ## ## ### ##### ############## ####### ##### ##### ##### ### ######
16+
# ## ### ## ####### ###### ############### #### ## ## # ###
17+
# ## ###### ####### ##### ################# ###
18+
# ## ### ## ## ### ################# ##
19+
# ## ## ################### ##
20+
# ## ### ######################
21+
# ## # ## #
22+
# ##
23+
# author: RaPoSpectre
24+
# time: 2016-11-25
25+
26+
# Definition for singly-linked list.
27+
class ListNode(object):
28+
def __init__(self, x):
29+
self.val = x
30+
self.next = None
31+
32+
33+
class Solution(object):
34+
def mergeTwoLists(self, l1, l2):
35+
"""
36+
:type l1: ListNode
37+
:type l2: ListNode
38+
:rtype: ListNode
39+
"""
40+
p1 = l1
41+
p2 = l2
42+
lr = ListNode('head')
43+
pr = lr
44+
while p1 or p2:
45+
if p1 and p2:
46+
if p1.val >= p2.val:
47+
pr.next = p2
48+
p2 = p2.next
49+
else:
50+
pr.next = p1
51+
p1 = p1.next
52+
elif p1 and not p2:
53+
pr.next = p1
54+
p1 = p1.next
55+
elif p2 and not p1:
56+
pr.next = p2
57+
p2 = p2.next
58+
pr = pr.next
59+
return lr.next
60+
61+
# l1 = ListNode(1)
62+
# l2 = ListNode(2)
63+
# l3 = ListNode(3)
64+
# l1.next = l2
65+
# l2.next = l3
66+
#
67+
# l4 = ListNode(2)
68+
# l5 = ListNode(5)
69+
# l4.next = l5
70+
#
71+
# lr = Solution().mergeTwoLists(l1, l4)
72+
#
73+
# while lr:
74+
# print lr.val
75+
# lr = lr.next

0 commit comments

Comments
 (0)
0