-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_4.py
More file actions
58 lines (46 loc) · 1.12 KB
/
question_4.py
File metadata and controls
58 lines (46 loc) · 1.12 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# @Time : 2018/5/5 下午5:34
# @Author : cancan
# @File : question_4.py
# @Function : 合并两个有序链表
"""
Question:
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
Example:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
"""
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1:
return l2
elif not l2:
return l1
t = [l1, l2]
n = l1.next
while n:
t.append(n)
n = n.next
n = l2.next
while n:
t.append(n)
n = n.next
t = sorted(t, key=lambda x: x.val)
r = t[0]
p = r
for i in t:
p.next = i
p = i
else:
p.next = None
return r