Question

Write an algorithm to check equality of two link lists. IN JAVA OR PSEUDOCODE

Write an algorithm to check equality of two link lists. IN JAVA OR PSEUDOCODE

Homework Answers

Answer #1

This is the pseudocode for the algorithm. We assume that each node of the linkedlist is of the form (data,next), where data is the value of that node and next contains the pointer to the next element of the linkedlist.

Our function returns false is the two linked lists are not equal else we return true.

We first check if the length of both the lists are 0, if they are 0 then we return true.

Then we iterate over each node to check if they are equal. Finally we check if both the linkedlist has the same end point. If they don't have the same endpoint, we return false.

function check_equality(list1, list2){
     if( list1 == none and list2 == none):
         return true
     
     head1 = list1
     head2 = list2

     while( head1 != none and head2 != none):
          if( head1.data != head2.data):
                return false
          else:
                head1 = head1.next
                head2 = head2.next
     
     if( head1 != none or head2 != none):
          return false

     return true
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in...
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in O(N) time. You can use as much extra space as you need. The original list pointers CAN NOT BE MODIFIED. State in big-O notation how much extra space is used by this algorithm. Write another iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse using O(1) extra space. The original list pointers CAN NOT BE MODIFIED. This algorithm can have...
Write a program that uses Standard Library algorithm to merge two ordered lists of strings into...
Write a program that uses Standard Library algorithm to merge two ordered lists of strings into a single ordered list of strings, then displays the resulting list.
write an algorithm ( pseudocode in c) which counts the number of element larger than P...
write an algorithm ( pseudocode in c) which counts the number of element larger than P bit smaller than Q.
Write a pseudocode algorithm for giving all employees in a company a cost-of-living wage increase of...
Write a pseudocode algorithm for giving all employees in a company a cost-of-living wage increase of 3.2%. Assume that the payroll file includes all current employees.
Write pseudocode for a simple algorithm for addition of two n-digit numbers (one of them could...
Write pseudocode for a simple algorithm for addition of two n-digit numbers (one of them could be < n digits with 0's appended to the left) in base-10, as follows. Assume the digits are stored in arrays A and B, with A[1] and B[1] being the rightmost digits and A[n] and B[n] being the leftmost digits. Use a for loop to go from right to left adding the digits and keeping track of the carry. Now, here's the real task:...
1.        A. Write a recursive brute force algorithm that calculates an. B. Write the java code...
1.        A. Write a recursive brute force algorithm that calculates an. B. Write the java code that does the calculation. C. What is the recurrence relation for the number of multiplications? D. What is the efficiency class of the algorithm?
Write a Java Program to bubble sort 10,20,15,0,6,7,2,1,-5,55. Including Algorithm flowchart of the program.
Write a Java Program to bubble sort 10,20,15,0,6,7,2,1,-5,55. Including Algorithm flowchart of the program.
IN JAVA One possible application of a Stack is to check for balanced parentheses or braces...
IN JAVA One possible application of a Stack is to check for balanced parentheses or braces in an expression. An expression is considered to be balanced if:  There are an equal number of open and close parantheses; AND  Every closed parenthesis seen (when parsing the expression from left to right) must be paired with a previously seen open parenthesis. Write an algorithm (not code, an algorithm) for how you would use a stack to check for balanced parentheses....
Give an algorithm for finding the second largest integer in a finite sequence of integers. Write...
Give an algorithm for finding the second largest integer in a finite sequence of integers. Write the algorithm in pseudocode format
Write a pseudocode for the following java programs: public class Item {    private String name;...
Write a pseudocode for the following java programs: public class Item {    private String name;    private double cost;    public Item(String name, double cost) {        this.name = name;        this.cost = cost;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getCost() {        return cost;    }    public void setCost(double cost) {...