-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21-MergeTwoSorted.cs
42 lines (36 loc) · 1.18 KB
/
21-MergeTwoSorted.cs
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
if(not list1 and not list2): return
if(not list1): return list2
if(not list2): return list1
pointer = None
if(list2.val < list1.val):
pointer = list2
list2 = list2.next
else:
pointer = list1
list1 = list1.next
head = pointer
while (list1 and list2):
if(list1.val < list2.val):
pointer.next = list1
pointer = pointer.next
list1 = list1.next
else:
pointer.next = list2
pointer = pointer.next
list2 = list2.next
while(list1):
pointer.next = list1
pointer = pointer.next
list1 = list1.next
while(list2):
pointer.next = list2
pointer = pointer.next
list2 = list2.next
return head