mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
27 lines
595 B
Python
27 lines
595 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
|
|
class Node:
|
|
def __init__(self, val=0, next=None):
|
|
self.val = val
|
|
self.next = next
|
|
|
|
|
|
def merge_two_list(list1: Optional[Node], list2: Optional[Node]) -> Optional[Node]:
|
|
|
|
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
|
|
|