
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Linked List of Maximum Difference of Squares from Given Linked List
A linked list is a linear data structure that consists of nodes and each node is not present in the contiguous form in the main memory, instead, each node contains the address of the next node. We are given a linked list of even length, we have to create a new linked list that contains half number of nodes as the given node, and the value of each node contains the difference between the square of the nodes of the given linked list in decreasing order.
Example
Let's understand the problem with the help of an input-output example ?
Input
Given linked list: 1 -> 4 -> 6 -> 9 -> 2 -> 5 -> null
Output
80 -> 32 -> 9 -> null
Explanation
If we sort all the elements we will get 1, 2, 4, 5, 6, 9 and then we will make pair from first element to last element, second element to second last element and so on.
Approach
In this problem, we need to find the maximum difference between the squares in the decreasing order, so, to get the maximum difference we need to sort the elements of the linked list and first and last element will have the most difference in the squared values.
First, we will create a class that will consist of the integer variable and a pointer to hold the value of a node and the pointer to the next node.
We will define some basic functions such print function to print all the elements of the linked list and function to create the linked list.
In the main function, we will create the linked list and then will call to the helper function which will return the head of the new linked list.
In the helper function, first we will define a deque that will contain all the elements of the linked list and then we will sort the deque.
As we need, first and the last element for difference, so deque is best option because we can pop the element from both ends, also we can use vector here and by using two pointers we can handle that.
After sorting we will iterate for required number of move to create the new linked list and will return its head.
Example
#include <bits/stdc++.h> using namespace std; // Linked list node class Node{ public: int data; // variable to store data Node* next; // pointer to store the address of next element // constuctor to create a new object or node Node(int val){ data = val; next = nullptr; } }; // function to add elements in the linked list Node* push(Node* head, int val){ // create a new node Node* new_node = new Node(val); // add the data of the next node new_node->next = head; // move the head to next position head = new_node; return head; } // function to print all the elements of the linked list void print(Node* head){ Node* temp = head; // assign head to temporary variable // iterating over the linked list using temp variable while(temp){ // print current data cout << temp->data << " -> "; // iterate to next pointer temp = temp->next; } cout<<"NULL"<<endl; } // function to get new linked list Node* getLL(Node* head){ // Stores the node of linekd list in deque to iterate later deque<int> dq; Node* temp = head; // store the data in head while(temp != nullptr){ dq.push_back(temp->data); temp = temp->next; } // sort the current dq sort(dq.begin(), dq.end()); // new_head is the head of the new linked list Node* new_head = nullptr; Node* prev_node = nullptr; // getting size of new linked list int sz = dq.size() / 2; // traversing over the size while (sz--) { int front = dq.front(); int back = dq.back(); // getting the differnce of squares int cur = back*back - front*front; // adding new value to new linked list Node* temp = new Node(cur); if(new_head == nullptr){ new_head = temp; prev_node = temp; } else { prev_node->next = temp; prev_node = temp; } // remove the first and last elements of the deque dq.pop_back(); dq.pop_front(); } // return the new linked list head return new_head; } int main(){ struct Node* head = NULL; // Given Linked list head = push(head, 9); head = push(head, 5); head = push(head, 6); head = push(head, 2); head = push(head, 4); head = push(head, 1); cout<<"The given linked list is: "<<endl; print(head); // calling to function to get the new linked list Node* newLL = getLL(head); // print the new Linked list cout<<"The new linked list is: "<<endl; print(newLL); return 0; }
Output
The given linked list is: 1 -> 4 -> 2 -> 6 -> 5 -> 9 -> NULL The new linked list is: 80 -> 32 -> 9 -> NULL
Time and Space Complexity
The time complexity of the above code is O(N*log(N)), where N is the size of the given linked list and we are getting logarithmic factor due to sorting the deque.
The space complexity of the above code is O(N), as we are using extra space of deque to store the values.
Conclusion
In this tutorial, we have implemented a program to create a linked list from the even size given linked list to half of its size. In new linked list each node is consist of the difference of the squares of the node values in the decreasing order. We have used the deque to store all the elements and then we have sorted it to get first and last element of the deque. The time complexity of the code is O(N*log(N)) and space complexity is linear.