Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Check two Linked Lists are Identical or not

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 503
    Comment on it

    Program to demonstrate two Linked Lists are Identical or not.

    class LinkedList
    {
    	Node head; // head of list
    
    	/* Linked list Node*/
    	class Node
    	{
    		int data;
    		Node next;
    		Node(int d) { data = d; next = null; }
    	}
    
    	/* Returns true if linked lists a and b are identical,
    	otherwise false */
    	boolean areIdentical(LinkedList secondlist)
    	{
    		Node x = this.head, y = secondlist.head;
    		while (x != null && y != null)
    		{
    			if (x.data != y.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 */
    			x = x.next;
    			y = y.next;
    		}
    
    		// If linked lists are identical, then 'a' and 'b' must
    		// be null at this point.
    		return (x == null && y == null);
    	}
        //pushing data into LinkedList
        
    	void push(int new_data)
    	{
    	    //creating a node with passed data
    		Node new_node = new Node(new_data);
            //next field of ne node is filled with head value
    		new_node.next = head;
    		//head now points to new node
    		head = new_node;
    	}
    
    	public static void main(String args[])
    	{
    		LinkedList list1 = new LinkedList();
    		LinkedList list2 = new LinkedList();
    
    		/* The constructed linked lists are :
    		llist1: 3->2->1
    		llist2: 3->2->1 */
    
    		list1.push(1);
    		list1.push(2);
    		list1.push(3);
    
    		list2.push(1);
    		list2.push(2);
    		list2.push(3);
    
    		if (list1.areIdentical(list2) == true)
    			System.out.println("Identical ");
    		else
    			System.out.println("Not identical ");
    
    	}
    } 
    

    Output:

    Identical 

    Explanation:

    In the above program a class LinkedList is created,within LinkedList class and innerclass Node is created with two fields data and next field. From main function ,two LinkedList list1 and list2 are created and for each LinkedList push function is called. Push function is called with a data parameter and within push function a new node is created for calling linkedlist containing passed data in data field.

    After creating two LinkedList, function areIdentical is called to compare two linkelist whether they are identical or not. areIdentical function is called by list1 and list2 is passed as an argument to it. Within areIdentical function a while loop runs till either of the list is null and within loop condition is checked if data of calling linkedlist and passed linkedlist are not equal then false is returned otherwise list point to next field. Finally if while loop does not return false then function returns a boolean value by checking condition whether two list have become null.  

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: