From 40c0dd37dd34d6a3c38bee848a854b2a0848daad Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptography@users.noreply.github.com> Date: Mon, 31 Jul 2023 13:44:12 -0700 Subject: [PATCH] Create merge_two_lists.py --- linked_lists/merge_two_lists.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 linked_lists/merge_two_lists.py diff --git a/linked_lists/merge_two_lists.py b/linked_lists/merge_two_lists.py new file mode 100644 index 0000000..7c48bba --- /dev/null +++ b/linked_lists/merge_two_lists.py @@ -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 +