mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create merge_two_lists.py
This commit is contained in:
parent
914d1eda87
commit
40c0dd37dd
26
linked_lists/merge_two_lists.py
Normal file
26
linked_lists/merge_two_lists.py
Normal file
@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
|
||||
class ListNode:
|
||||
def __init__(self, val=0, next=None):
|
||||
self.val = val
|
||||
self.next = next
|
||||
|
||||
|
||||
def merge_two_list(list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
|
||||
|
||||
if not list1:
|
||||
return list2
|
||||
|
||||
if not list2:
|
||||
return list1
|
||||
|
||||
if list1.val < list2.val:
|
||||
list1.next = merge_two_list(list1.next, list2)
|
||||
return list1
|
||||
else:
|
||||
list2.next = merge_two_list(list1, list2.next)
|
||||
return list2
|
||||
|
Loading…
x
Reference in New Issue
Block a user