Explanation of Solution
The algorithm to compare two circularly linked lists “L” and “M” are same sequence of elements given below:
Algorithm:
Input: Two circularly linked list “L” and “M”.
Output: Return true when two circular linked lists “L” and “M” are same sequence of elements. Otherwise, return false.
equal(L :Circularly linked list, M Circularly linked list):
//Create new node for list "L" and "M"
Create a new node "a" and "b"
/*Call getHead() method using circularly linked list "L" to assign the head of list as "a". */
a = L.getHead();
/*Call getHead() method using circularly linked list "M" to assign the head of list as "b". */
b = M.getHead();
/*Loop executes until the next node of list is not equal to "null" for both lists. */
while (a.getNext()!= null || b.getNext()!= null)
{
/*Loop executes until both list elements are not equal. */
while(a.getElement() != b.getElement())
//Assign next node as "b"
b = b.getNext();
//Assign next node as "a"
a = a.getNext();
/*Call getHead() method using circularly linked list "M" to reassign the head of list as "b". */
b = M...
Trending nowThis is a popular solution!
Chapter 3 Solutions
Data Structures and Algorithms in Java
- n=6 and A=(3,5,4,1,3,2). Draw only the right half of the corresponding walkthrough as shown in P.155, showing only the list A at the endarrow_forwardThe intersection of two sets contains the list of elements that are common to both, without repetition. As such, you are required to write an algorithm that finds and returns the intersection of two sets A and B. The sets A and B and the resulting intersection set are all lists. Assume the size of A is n and that of B is m. (a) Write an iterative algorithm that finds the intersection. (b) Prove the correctness of your algorithm; i.e. state its loop invariant and prove it by induc- tion. (c) Analyze the time complexity of your algorithm. (d) Think of a way to enhance the complexity of your algorithm by sorting both lists A and B before calculating their intersection. Write the enhanced version and prove that it has a better time complexity.arrow_forwardLet's say we have an algorithm that runs in Θ(n5) and another algorithm that runs in Θ(n3). Would there be any way that the first one would run faster for certain list sizes or in any other way? Please, provide explanation and possible examples. Thank you.arrow_forward
- C Write a recursive algorithm mypowerlist (n) that, given a nat- ural number n as input, returns the "powerlist" (i.e. the power- set expressed as a list) of {1, ...,n}. So the output of mypowerlist (2) should be the following list of sets: [set(),{1},{2}, {1,2}]. Hint: it is obvious what the solution is for n=0. For n+1 note that there are two kinds of subset: those containing n+1, and those not. The latter can be obtained immediately by calling mypowerset (n). How can you obtain the first group?arrow_forwardAzra wants to check a list contains a even number or not. But she does not have knowledge about the loops . So your task without using loop check if a list has a single even element or not and print according to that. In elixir programming languagearrow_forwardGiven a singly linked list, reverse the list. This means you have to reverse every node. For example if there are 4 nodes, the node at position 0 will move to position 3, the node at 1 will move to position 2 and so on. You do not have to write the Node class just write what you have been asked to. You are NOT allowed to create a new list. In python languagearrow_forward
- 1. Analyze the word finder with wild card characters scenario to form the algorithm and compute the time complexity. You are given a list of characters with size n, m. You need to read character data provided as input into this list. There is also a list of p words that you need to find in the given problem. You can only search in left, right, up and down direction from a position. There are two wild characters *' and '+' ,if there is any instance of wild card characters for example '+' appears you can match whatever character in your word that you are searching at this position (only one character). Similarly, if you got wildcard *' you can skip any number of characters ( A sequence of characters not necessary same in between the characters) but the last character should be matched in the given data. This means you are not allowed to match a first and last character with *' wildeard. Input: The first line of the input will give you two integers n and m. From the next line you will get…arrow_forwardThe median value of a set of n numbers is the value that separates the half of higher values from the half of lower values in the set. The median can be found by arranging the values in the set in order and choosing the “middle” value. See the lecture slides for some sorting functions we will talk about tomorrow that will get any list in order. If there are an even number of values in the set, the median is described as the mean of the two middle values. (b) Write a SCHEME function, named list-median, that takes a list of numbers as a parameter and returns the median value in the list.arrow_forwardGiven a circularly linked list L containing an even number of nodes, de-velop java application how to split L into two circularly linked lists of halfthe size.arrow_forward
- create a program that takes a nested list and flattens the nested list without using recursion.arrow_forward3. Implement both Prim's algorithm and Kruskal's algorithm. Compare therunning times of your programs on a set of "random" graphs.4. Write a program to find all the connected components of a graph.5. Consider the following list of integers: [1,2,3,4,5,6,7,8,9,10]. Show how this list is sorted by the following algorithms:• bubble sort• selection sort• insertion sort• shell sort (you decide on the increments)• merge sort• quick sort (you decide on the pivot valuearrow_forwardWrite an algorithm that sorts a list of n items by dividing it into three sublistsof about n/3 items, sorting each sublist recursively and merging the threesorted sublists. Analyze your algorithm, and give the results under orderarrow_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