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_forwardGiven a singly linked list of integers, reverse the nodes of the linked list 'k' at a time and return its modified list. 'k' is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of 'k,' then left-out nodes, in the end, should be reversed as well.Example :Given this linked list: 1 -> 2 -> 3 -> 4 -> 5 For k = 2, you should return: 2 -> 1 -> 4 -> 3 -> 5 For k = 3, you should return: 3 -> 2 -> 1 -> 5 -> 4 Note :No need to print the list, it has already been taken care. Only return the new head to the list. Input format :The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the singly linked list separated by a single space. The second line of input contains a single integer depicting the value of 'k'. Remember/Consider :While specifying the list…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
- Find 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_forwardCHECK THE IMAGE FOR QUESTIONarrow_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_forwardGiven 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_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
- Write a java program class for a singly linked list with Insertion from head, tail and middlearrow_forwardConsider an unordered list L[0:5] = {23, 14, 98, 45, 67, 53} of data elements. Letus search for the key K = 53. Obviously, the search progresses down the listcomparing key K with each of the elements in the list until it finds it as the lastelement in the list. In the case of searching for the key K = 110, the searchprogresses but falls off the list thereby deeming it to be an unsuccessful search. Write Algorithm Procedure for ordered and unordered linear search and write their time complexityarrow_forwardwrite a function to print alternate nodes of the given double linked list , first from head to end , and then from end to headarrow_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