-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoSortedLists.py
More file actions
43 lines (40 loc) · 1.07 KB
/
Copy pathMergeTwoSortedLists.py
File metadata and controls
43 lines (40 loc) · 1.07 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
"""
@Project: leetcode
@file: MergeTwoSortedLists.py
@author: AC
@time: 2016/5/6 12:50
@Description: 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.
"""
# 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
"""
s, t = l1, l2
head = ListNode(0)
temp = head
while s is not None and t is not None:
if s.val < t.val:
temp.next = s
s = s.next
else:
temp.next = t
t = t.next
temp = temp.next
while s is not None:
temp.next = s
s = s.next
temp = temp.next
while t is not None:
temp.next = t
t = t.next
temp = temp.next
return head.next