From 20fa85ed45f79ee890148c246c61f1e692cbcd78 Mon Sep 17 00:00:00 2001 From: ChikkalaSukrutiNaidu <24b01a1222@svecw.edu.in> Date: Fri, 17 Oct 2025 18:36:24 +0530 Subject: [PATCH] Create Solution.java --- Solution.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Solution.java diff --git a/Solution.java b/Solution.java new file mode 100644 index 0000000..e91a585 --- /dev/null +++ b/Solution.java @@ -0,0 +1,24 @@ + +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode dummy = new ListNode(0); + ListNode current = dummy; + + while (list1 != null && list2 != null) { + if (list1.val < list2.val) { + current.next = list1; + list1 = list1.next; + } else { + current.next = list2; + list2 = list2.next; + } + current = current.next; + } + + // Attach the remaining nodes from the non-exhausted list + if (list1 != null) current.next = list1; + else current.next = list2; + + return dummy.next; // Return the head of the merged list + } +}