Skip to content

Commit 161edcc

Browse files
committed
21. Merge Two Sorted Lists solution
1 parent f978922 commit 161edcc

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

merge-two-sorted-lists/doh6077.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# 21. Merge Two Sorted Lists
3+
4+
class Solution:
5+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
6+
tempNode = ListNode()
7+
node = tempNode
8+
while list1 and list2:
9+
if list1.val < list2.val:
10+
node.next = list1
11+
list1 = list1.next
12+
else:
13+
node.next = list2
14+
list2 = list2.next
15+
node = node.next
16+
node.next = list1 or list2
17+
return tempNode.next

0 commit comments

Comments
 (0)