how to concatenate two singly linked lists in a way that we add all nodes in the
ID: 3888085 • Letter: H
Question
how to concatenate two singly linked lists in a way that we add all nodes in the second one to the first one without changing the second one at all. consider cases in which one of them is empty. I want the solution to be a function that takes two nodes as the heads for each list-In java in the same steps in the picture.
how to make sure that the coming parameters are head reference of linked lists? I thinkit is different that making sure that they are not empty lists.
Preconditions 11 and 12 are head reference of linked lists Both lists are not empty Postconditions A copy of list 12 1s concatenated (added to the end NOTE The nodes added to the list 11 must be copies of the /7 of list 11List 12 should be unchanged by the function /nodes in 1ist 12Explanation / Answer
Hi friend, You have not posted the structure of linked list.
I have assumed that :
public class iNode {
public int item;
public iNode next;
}
Then required function:
public iNode concatenate(iNode first, iNode second) {
// checking base condition
if(first == null)
return second;
// appending second list in first
iNode temp = first;
while(temp.next != null)
temp = temp.next;
temp.next = second; // appending head of second to first
return first;
}
Please let me know in case of any issue
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.