Explanation of Solution
The algorithm to concatenate two doubly linked list “L” and “M” into single list “L'” is given below:
Algorithm:
Input: Two doubly linked list “L” and “M”.
Output: Concatenate the two doubly linked lists into single list “L'”.
Concatenate(L, M):
//Create new nodes
Create a new node "n" and "v"
/*Get previous node for trailer of list "L" using getPrev() method and assign into "n". */
n = (L.getTrailer()).getPrev()
/*Get next node for header of list "M" using getNext() method and assign into "v" */
v = (M.getHeader()).getNext()
// Set next node for header of list "M" as "null"
(M.getHeader()).setNext(null)
//Set previous node for trailer of list "L" as "null"
(L.getTrailer()).setPrev(null)
/*Call setNext() method by passing the "v" node to set the next node as "v". */
n.setNext(v)
/*Call setPrev() method by passing the "n" node to set the previous node as "n". */
v.setPrev(n)
//Assign the concatenated list into single list "L'"
L' = L
/*Set trailer for list "L'" by calling setTrailer() method by passing parameter as "trailer of list "M"...
Want to see the full answer?
Check out a sample textbook solutionChapter 3 Solutions
Data Structures and Algorithms in Java
- What are the requirements for determining if a linked list T is empty if T is one of the following: (i) a simple singly linked list, (ii) a headed singly linked list, (iii) a simple circularly linked list, or (iv) a headed circularly linked list?arrow_forwardComputer Science please solve this problem as soon as possible Write a function to insert the element into the linked list the given number X. E.g., for a given set of integers, [2, 4, 12, 6, 5, 3], the linked list is 2->4->12->6->5->3. For a given number, (X=15), the count will be 11 that are (2,4), (2,12), (2,6), (2,5), (2,3), (4,6), (4,5), (4,3), (6,5), (6,3), (5,3).arrow_forwardConsider an unordered list L[0:5] = {23, 14, 98, 45, 67, 53} of data elements. Let us search for the key K = 53. Obviously, the search progresses down the list comparing key K with each of the elements in the list until it finds it as the last element in the list. In the case of searching for the key K = 110, the search progresses but falls off the list thereby deeming it to be an unsuccessful search. Write procedure code for LINEAR_SEARCH_ORDERED and UNORDEREDarrow_forward
- Create two singly linked lists A and B to represents elements in the following set A and B respectively. 3. A = { 5, 6, 8, 9, 10, 11} B = { 1, 6, 2, 9, 3, 11, 4} Develop an algorithm and implement the same to represent only intersection of two lists A and B and print the same.arrow_forwardFind the existence of an intersection between two (singly) linked lists. Send back the node that intersects. Keep in mind that reference, not value, is used to define the intersection. This means that they are intersecting if the kth node of the first linked list is the exact same node (by reference) as the jth node of the second linked list.arrow_forwardSuppose there are two singly linked lists both of which intersect at some point and become a single linked list. The head or start pointers of both the lists are known, but the intersecting node is unknown. Also, the number of nodes in each of the list before they intersect are unknown and both the list may have it different. List1 may have n nodes before it reaches intersection point and List2 might have m nodes before it reaches intersection point where m and n may be m = n, m > n or m < n. Give an algorithm for finding the merging point. Hints: A brute force approach would be to compare every pointer in one list with every pointer in another list. But in this case the complexity would be O(mn)arrow_forward
- Given the head of a singly linked list of integers, write the function to arrange the elements such thatall the even numbers are placed after all the odd numbers. The relative order of the odd and eventerms should remain unchanged.Input Format:Elements of linked listOutput Format:Resultant linked listSample Input 1:1 4 5 2Sample Output 1:1 5 4 2Sample Input 2:1 11 3 6 8 0 9Sample Output 2:1 11 3 9 6 8 0 in javaarrow_forwardArray lists and linked lists are both implementations of lists. Give an example of a situation where an array list would be the better choice and one where a linked list would. Explain the reasons in each case.arrow_forwardGiven two (singly) linked lists, determine if the two lists intersect. Return the intersecting node. Note that the intersection is defined based on reference, not value. That is, if the kthnode of the first linked list is the exact same node (by reference) as the jth node of the secondlinked list, then they are intersecting.arrow_forward
- Given a circular linked list, implement an algorithm that returns the node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, soas to make a loop in the linked list.EXAMPLEInput:Output:A -> B -> C -> D -> E -> C [the same C as earlier]Carrow_forwardWrite a java program class for a singly linked list with Insertion from head, tail and middlearrow_forwardGiven a circular linked list, implement an algorithm that returns the node at thebeginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, soas to make a loop in the linked list.EXAMPLEInput: A -> B -> C -> D -> E -> C [the same C as earlier]Output: Carrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education