File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ # Definition for singly-linked list.
2+ # class ListNode:
3+ # def __init__(self, val=0, next=None):
4+ # self.val = val
5+ # self.next = next
6+ class Solution :
7+ def mergeTwoLists (self , list1 : Optional [ListNode ], list2 : Optional [ListNode ]) -> Optional [ListNode ]:
8+ head = ListNode ()
9+ result = head
10+
11+ while list1 and list2 :
12+ if list1 .val <= list2 .val :
13+ head .next = ListNode (list1 .val )
14+ head = head .next
15+ list1 = list1 .next
16+ else :
17+ head .next = ListNode (list2 .val )
18+ head = head .next
19+ list2 = list2 .next
20+
21+ while list1 :
22+ head .next = ListNode (list1 .val )
23+ head = head .next
24+ list1 = list1 .next
25+
26+ while list2 :
27+ head .next = ListNode (list2 .val )
28+ head = head .next
29+ list2 = list2 .next
30+
31+ return result .next
You can’t perform that action at this time.
0 commit comments