Question

In: Computer Science

How would you show two linked lists are equal? (Java for Data Structures and Algorithms)

How would you show two linked lists are equal? (Java for Data Structures and Algorithms)

Solutions

Expert Solution

Below is the complete Java solution. If you face any difficulty while understanding it, Please let me know in the comments.

Approach:

Two linked lists are equal if they have the same data and arrangements. So, We need to traverse both the linked lists parallely and check if the corresponding node data are equal or not. If their exists even a single node whose values doesn't match, then linked lists are not equal.

Refer below codes to get the complete concept.

Code:

import java.util.*;

public class Main {
  
// Node to represent linked list
static class ListNode {
int data;
ListNode next;
  
ListNode (int data) {
this.data = data;
this.next = null;
}
}
  
// Function to check if two linked lists are equal or not
static boolean areLinkedListsEqual(ListNode head1, ListNode head2) {
  
// Run loop till both the head nodes doesn't becomes null
while (head1 != null && head2 != null ) {
  
// if node vales are not equal, means linked lists are not equal, so return false
if(head1.data != head2.data)
return false;
  
// move head pointers to next node
head1 = head1.next;
head2 = head2.next;
}
  
// if both the heads are not null, it means linked lists are not equal
return (head1 == null && head2 == null);
}
  
   public static void main(String[] args) {
   // create two linked lists
       ListNode head1 = new ListNode(5);
       head1.next = new ListNode(2);
      
       ListNode head2 = new ListNode(5);
       head2.next = new ListNode(2);
      
       // check if the linked lists are equal or not
       System.out.println(areLinkedListsEqual(head1,head2));
   }
}

Screenshot:

Output:


Related Solutions

JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to...
JAVA *** All data structures, including array operations, queues, stacks, linked lists, trees, etc need to be implemented by you. Write a menu driven program that implements the following Binary Search Tree Operations FIND (item) INSERT (item) DELETE (item) DELETE_TREE (delete all nodes - be careful with the traversal!)
This is a C++ based question that involves Data Structures and Algorithms. Q. Application: Linked List...
This is a C++ based question that involves Data Structures and Algorithms. Q. Application: Linked List of Bus Transit and Passengers You are to implement a C++ program for City Bus Transit using linked list data structure to maintain record of passengers. Specifically, you are to implement the following methods/functions: For Passenger: o A function which can create a new node of the linked list using new for each newpassenger o A function that prints the time of single passenger...
04 Prove : Homework - Data Structures Linked Lists Outcomes At the end of this study,...
04 Prove : Homework - Data Structures Linked Lists Outcomes At the end of this study, successful students will be able to: Articulate the strengths and weaknesses of Linked Lists. Use Linked Lists in Python to solve problems. Preparation Material Read the following sections from Wikipedia: Linked Lists: The Introduction Advantages Disadvantages Basic concepts and nomenclature The following subsections are sufficient: Intro Singly Linked Lists Doubly Linked Lists Tradeoffs The following subsections are sufficient: Linked Lists vs. Dynamic Arrays Data...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
2.1 Linked Lists Linked lists are an example of an unbound data structure. Whereas the array...
2.1 Linked Lists Linked lists are an example of an unbound data structure. Whereas the array is based around having a fixed size per array creation, there is no logical limit on the ability of a linked list to grow. Physical limitations aside, linked lists are capable of growing largely without any limitations. To achieve this, they trade easier access to their individual elements. This is because linked lists have to be traversed from their root node to any node...
(Subject: Data Structures and Algorithms) Faster merging. You are given two sorted sequences of $\lg{n}$ and...
(Subject: Data Structures and Algorithms) Faster merging. You are given two sorted sequences of $\lg{n}$ and $n-1$ keys. We would like to merge those two sorted sequences by performing $o(n)$ comparisons. (Note we are interested in comparisons, not running time.) Show how this can be done or argue that it cannot be done. Note: In class we showed that ordinary merging would require no more than $\lg{n}+n-1+1= n + \lg{n}$ comparisons.
1. Lisp only has two data structures, what are they and how can a doubly linked...
1. Lisp only has two data structures, what are they and how can a doubly linked list be created using them? 2. In a language with strong typing, what advantages does static checking have over dynamic checking?
Java Linked Lists I want a simple program that reads two text files that contains an...
Java Linked Lists I want a simple program that reads two text files that contains an integers matrix and store each file into a linked lists matrix so I can later preform operations such as addition and subtraction on the matrices an example of the input text files: sample a 2 6 2 6 2 18 17 11 20 sample b 3 13 5 4 11 20 13 18 20
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
Mergesort is known to be one of the fastest algorithms for sorting lists of data. In...
Mergesort is known to be one of the fastest algorithms for sorting lists of data. In fact, the runtime of mergesort is proportional to n· logn where n is the size of the list to be sorted. Bubblesort, on the other hand is a much slower algorithm, since it's runtime is proportional to n2 , where n is the size of the list to be sorted. As an experiment, mergesort is run on a slow computer, bubblesort is run on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT