Merge Two Sorted Lists

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists into 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.
LeetCode Problem - 21
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
// Initialize a list to store the values from both linked lists
List<Integer> list = new ArrayList<>();
// Traverse the first linked list and add its values to the list
ListNode tempHead = list1;
while (tempHead != null) {
list.add(tempHead.val);
tempHead = tempHead.next;
}
// Traverse the second linked list and add its values to the list
tempHead = list2;
while (tempHead != null) {
list.add(tempHead.val);
tempHead = tempHead.next;
}
// If both lists are empty, return null
if (list.isEmpty()) return null;
// Sort the list to merge the two linked lists in sorted order
Collections.sort(list);
// Create a new linked list from the sorted values
ListNode answer = new ListNode(list.get(0));
ListNode answerHead = answer;
// Iterate through the sorted list and add nodes to the answer list
for (int i = 1; i < list.size(); i++) {
answerHead.next = new ListNode(list.get(i));
answerHead = answerHead.next;
}
// Return the head of the merged linked list
return answer;
}
}




