Data Structures and Algorithms: Write reasonably syntactically correct code: (a
ID: 3840980 • Letter: D
Question
Data Structures and Algorithms: Write reasonably syntactically correct code: (a diagram of your "list" and "nodes" is helpful!) Given the following definitions for a sorted linked list: Public Class LListSorted {private LnodeHead;} Class Lnode{Object data: Lnode next;} Write a method in the LListSorted class that will accept as parameters two sorted lists}lst1 and 1st2, and create a new list that will merge the elements of 1st I and lst2 into a single sorted linked list. The method should return the new list. Your code will be graded on correctness and efficiency of the code. Please include the time complexity of your method.Explanation / Answer
the method can be like as below
LNode MergeTwoLists (LNode lst1, LNode lst2) {
//check for the null condition of linked list
if (lst1 == null) return lst2;
if (lst2 == null) return lst1;
// if list 1 is smaller append list 2 else append list 1 in list 2
if (lst1.data < lst2.data) {
lst1.next = MergeLists(lst1.next, lst2);
return lst1;
} else {
lst2.next = MergeLists(lst2.next, lst1);
return lst2;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.