Check if two Linked Lists are permutations of each other

Given two singly Linked list of integer data. The task is to write a program that efficiently checks if two linked lists are permutations of each other.

Examples:

Input: 1 -> 2 -> 3 -> 4 -> 5
        2 -> 1 -> 3 -> 5 -> 4
Output: Yes

Input: 10 -> 20 -> 30 -> 40
        20 -> 50 -> 60 -> 70
Output: No


Approach: Do the following for both linked lists:

  1. Take a temporary node pointing to the head of the linked list.
  2. Start traversing through the linked list, and keep sum and multiplications of data of nodes.

Note : After having sum and multiplication of both linked list, check if sum and multiplication of both linked lists are equal. If they are equal, it means linked lists are permutations of each other, else not.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if linked lists
// are permutations of each other
#include <bits/stdc++.h>
  
using namespace std;
  
// A linked list node
struct Node {
    int data;
    struct Node* next;
};
  
/*Function to check if two linked lists
* are permutations of each other
* first : reference to head of first linked list
* second : reference to head of second linked list
*/
bool isPermutation(struct Node* first, struct Node* second)
{
  
    // Variables to keep track of sum and multiplication
    int sum1 = 0, sum2 = 0, mul1 = 1, mul2 = 1;
  
    struct Node* temp1 = first;
  
    // Traversing through linked list
    // and calculating sum and multiply
    while (temp1 != NULL) {
        sum1 += temp1->data;
        mul1 *= temp1->data;
        temp1 = temp1->next;
    }
  
    struct Node* temp2 = second;
  
    // Traversing through linked list
    // and calculating sum and multiply
    while (temp2 != NULL) {
        sum2 += temp2->data;
        mul2 *= temp2->data;
        temp2 = temp2->next;
    }
  
    return ((sum1 == sum2) && (mul1 == mul2));
}
  
// Function to add a node at the
// begining of Linked List
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
  
    /* put in the data */
    new_node->data = new_data;
  
    /* link the old list off the new node */
    new_node->next = (*head_ref);
  
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
  
// Driver program to test above function
int main()
{
    struct Node* first = NULL;
  
    /* First constructed linked list is: 
    12 -> 35 -> 1 -> 10 -> 34 -> 1 */
    push(&first, 1);
    push(&first, 34);
    push(&first, 10);
    push(&first, 1);
    push(&first, 35);
    push(&first, 12);
  
    struct Node* second = NULL;
    /* Second constructed linked list is: 
    35 -> 1 -> 12 -> 1 -> 10 -> 34 */
    push(&second, 35);
    push(&second, 1);
    push(&second, 12);
    push(&second, 1);
    push(&second, 10);
    push(&second, 34);
  
    if (isPermutation(first, second)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
  
    return 0;
}

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.