Question

In: Computer Science

Write an algorithm to check equality of two link lists.(java)

Write an algorithm to check equality of two link lists.(java)

Solutions

Expert Solution

1. Start iterating from the head node of the two linked list.

2. Compare the data part of the nodes of the two linked list.

3. If matched, move to the next node of the two linked list else return false.

4. Repeat step 2 and 3 until either of the linked list points to null.

5. If both the linked list points to null then return true otherwise return false.

IMPLEMENTATION

    /* Returns true if linked lists a and b are identical, 
       otherwise false */
    boolean areIdentical(LinkedList listb) 
    { 
        Node a = this.head, b = listb.head; 
        while (a != null && b != null) 
        { 
            if (a.data != b.data) 
                return false; 
  
            /* If we reach here, then a and b are not null 
               and their data is same, so move to next nodes 
               in both lists */
            a = a.next; 
            b = b.next; 
        } 
  
        // If linked lists are identical, then 'a' and 'b' must 
        // be null at this point. 
        return (a == null && b == null); 
    } 

Related Solutions

Write the algorithm and code that creates an array of link lists where the user determines...
Write the algorithm and code that creates an array of link lists where the user determines the number of rows and the number of nodes for each row. Needs to be in C++ language. IT IS DUE TONIGHT AT 11:59. HELP PLEASE Two-Dimensional Array Example: [0] [1] [2] [3] [0] 2 4 6 8 [1] 1 3 [2] 8 4 6 [3] 5
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
Write an algorithm for combining two skip lists in O(a + b) time, where a is...
Write an algorithm for combining two skip lists in O(a + b) time, where a is the number of keys in the first list, and b is the number of keys in the second list.
How to write Prim's Algorithm with min-Heap and adjacency Lists?
How to write Prim's Algorithm with min-Heap and adjacency Lists?
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should be implemented: Constant space, i.e. without creating extra copies of the linked list Unrestricted space q1: / For this implementation you should use constant space   // Note: you are free to add any extra methods,   // but this method has to be used   public static boolean isPalindromeRestricted(ListNode head) {     // Your code goes here     return false;   } q2: // For this implementation the space...
Given two lists, write python code to print “True” if the two lists have at least...
Given two lists, write python code to print “True” if the two lists have at least one common element. For example, x = [1,2,3], y=[3,4,5], then the program should print “True” since there is a common element 3.
Describe an optimal algorithm for finding the integers common to two lists of n integers each....
Describe an optimal algorithm for finding the integers common to two lists of n integers each. Evaluate how long each step in your algorithm takes using Θ-notation.
Write a program in JAVA using the BigInteger library that can be used to check a...
Write a program in JAVA using the BigInteger library that can be used to check a RSA signature, based on the signer's RSA public key pair. To test your program, take the following information about a message Alice signed and use the verify signature to reproduce the message Alice signed and convert it back to String format. n = 68236588817658935156357212288430888402056854883696767622850112840388111129987 e = 65537 signature = 46612763171375975923246342580942010388414761162366281695045830390867474569531
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
(JAVA) We will write a program to check the spelling of the word entered by user,...
(JAVA) We will write a program to check the spelling of the word entered by user, using data from a dictionary, which is text file. The program will ask user to enter a word for spell check, and then check in the text file (dictionary.txt) if the word exists. (The dictionary.txt file is provided) If the word exists in text file, the output will be “Word is correctly spelled”. If the word doesn’t exist in the text file, program will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT