mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 12:46:11 -04:00
27 lines
613 B
Python
27 lines
613 B
Python
#!/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
|
|
|