forked from tech-cow/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoSortedLists.py
More file actions
31 lines (25 loc) · 914 Bytes
/
MergeTwoSortedLists.py
File metadata and controls
31 lines (25 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Yu Zhou
# 21. Merge Two Sorted Lists
# Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists
#思路
#因为是sorted,只需要设置一个guard_node用来之后的return
#就可以设置一个新的Linkedlist的Node,然后用来储存
#比对完了的值,最后返回guard_node.next(跳过第一个dummy node)就行了
class Solution(object):
def mergeTwoLists(self, n1, n2):
#Edge
if not n1 and not n2:
return None
node = guard_node = ListNode(0)
while n1 and n2:
if n1.val <= n2.val:
node.next = n1
n1 = n1.next
else:
node.next = n2
n2 = n2.next
node = node.next
#Edge
if n1 or n2:
node.next = n1 or n2
return guard_node.next