From 236fca14c48bf4b8951a01679a246f4d1f8bbab7 Mon Sep 17 00:00:00 2001 From: yashal-ali Date: Fri, 25 Oct 2024 19:53:11 +0500 Subject: [PATCH] lined list merge two sorting list algo --- CONTRIBUTORS.md | 5 ++++- Python/mergeTwoSortedList.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 Python/mergeTwoSortedList.py diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index c6c487a0..352e9cbd 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,3 +1,4 @@ +

✨Happy Hacktoberfest 2023✨

Hacktoberfest is a yearly event which encourages not only programmers and coders but also non-technical background to contribute in open source and start their journey with open source.

Star Repo is Mandatory for Hactober Accepting!

@@ -52,4 +53,6 @@ | Ankita Sinha Ray |ankitasray| | Aditya Pandey |Aditya7pandey| | Yash Bandal |Yash Bandal| -| Harshit Jaiswal |Harshit Jaiswal| \ No newline at end of file +| Harshit Jaiswal |Harshit Jaiswal| +| Yashal Ali |Yashal Ali | + diff --git a/Python/mergeTwoSortedList.py b/Python/mergeTwoSortedList.py new file mode 100644 index 00000000..b5bade00 --- /dev/null +++ b/Python/mergeTwoSortedList.py @@ -0,0 +1,31 @@ +# Merge Two Sorted Lists +# You are given the heads of two sorted linked lists list1 and list2. +# Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. +# Return the head of the merged linked list. + +# Input: list1 = [1,2,4], list2 = [1,3,4] +# Output: [1,1,2,3,4,4] + +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def mergeTwoLists(self, list1, list2): + curr = dummy = ListNode() + while list1 and list2: + if list1.val < list2.val: + curr.next = list1 + list1 = list1.next + else: + curr.next = list2 + list2 = list2.next + curr = curr.next + + if list1: + curr.next = list1 + else: + curr.next = list2 + + return dummy.next \ No newline at end of file