Given a circular singly linked list, the task is to print the next greater element for each node in the linked list. If there is no next greater element for any node, then print “-1” for that node.
Examples:
Input: head = 1 ? 5 ? 2 ? 10 ? 0 ? (head)
Output: 5 10 10 -1 -1
Explanation:
The next greater elements of each node are:
- Node 1: Next greater element is 5.
- Node 5: Next greater element is 10.
- Node 2: Next greater element is 10.
- Node 10: No next greater element exists. Therefore print “-1”.
- Node 0: No next greater element exists. Therefore print “-1”.
Input: head = 1 ? 5 ? 12 ? 10 ? 0 ? (head)
Output: 5 12 -1 12 -1
Approach: The given problem can be solved by iterating the circular linked list to the right until the same element appears again and then printing the next greater element found for each node. Follow the steps below to solve the problem:
- Initialize a Node variable, say res as NULL to store the head node of the Linked List representing the next greater element.
- Also, initialize a Node variable, say tempList as NULL to iterate over the Linked List representing the next greater element.
- Iterate over the circular Linked List and perform the following steps:
- Initialize a Node, say curr to store the reference to the current node of the circular linked list.
- Initialize a variable say Val as -1 to store the value of the next greater element of the current node.
- Perform the following steps until curr is not equal to the reference of the current node of the circular Linked List:
- If the value at the current node curr is greater than the value of the current node of the circular Linked list then assign the value of curr.data to Val and break out of the loop.
- Update the value of curr node as curr = curr.next.
- If the node res is NULL then create a new node with the value Val and assign it to res, and then assign the value of res to tempList.
- Otherwise, create a new node with the value Val and assign it to tempList.next, and then update the node tempList as tempList = tempList.next.
- After completing the above steps, print the elements of the Linked List res as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *next;
Node( int d)
{
data = d;
next = NULL;
}
};
void print(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " " ;
temp = temp->next;
}
}
void NextGreaterElement(Node *head)
{
Node *H = head;
Node *res = NULL;
Node *tempList = NULL;
do
{
Node *curr = head;
int Val = -1;
do
{
if (head->data < curr->data)
{
Val = curr->data;
break ;
}
curr = curr->next;
} while (curr != head);
if (res == NULL)
{
res = new Node(Val);
tempList = res;
}
else
{
tempList->next = new Node(Val);
tempList = tempList->next;
}
head = head->next;
} while (head != H);
print(res);
}
int main()
{
Node *head = new Node(1);
head->next = new Node(5);
head->next->next = new Node(12);
head->next->next->next = new Node(10);
head->next->next->next->next = new Node(0);
head->next->next->next->next->next = head;
NextGreaterElement(head);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static Node head;
static class Node {
int data;
Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
static void print(Node head)
{
Node temp = head;
while (temp != null ) {
System.out.print(
temp.data + " " );
temp = temp.next;
}
}
static void NextGreaterElement(Node head)
{
Node H = head;
Node res = null ;
Node tempList = null ;
do {
Node curr = head;
int Val = - 1 ;
do {
if (head.data < curr.data) {
Val = curr.data;
break ;
}
curr = curr.next;
} while (curr != head);
if (res == null ) {
res = new Node(Val);
tempList = res;
}
else {
tempList.next = new Node(Val);
tempList = tempList.next;
}
head = head.next;
} while (head != H);
print(res);
}
public static void main(String[] args)
{
head = new Node( 1 );
head.next = new Node( 5 );
head.next.next = new Node( 12 );
head.next.next.next = new Node( 10 );
head.next.next.next.next = new Node( 0 );
head.next.next.next.next.next = head;
NextGreaterElement(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def Print (head):
temp = head
while (temp ! = None ):
print (temp.data,end = " " )
temp = temp. next
def NextGreaterElement(head):
H = head
res = None
tempList = None
while ( True ):
curr = head
Val = - 1
while ( True ):
if (head.data < curr.data):
Val = curr.data
break
curr = curr. next
if (curr = = head):
break
if (res = = None ):
res = Node(Val)
tempList = res
else :
tempList. next = Node(Val)
tempList = tempList. next
head = head. next
if (head = = H):
break
Print (res)
head = Node( 1 )
head. next = Node( 5 )
head. next . next = Node( 12 )
head. next . next . next = Node( 10 )
head. next . next . next . next = Node( 0 )
head. next . next . next . next . next = head
NextGreaterElement(head)
|
C#
using System;
class Node
{
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
class GFG{
static Node head;
static void print(Node head)
{
Node temp = head;
while (temp != null )
{
Console.Write(temp.data + " " );
temp = temp.next;
}
}
static void NextGreaterElement(Node head)
{
Node H = head;
Node res = null ;
Node tempList = null ;
do {
Node curr = head;
int Val = -1;
do {
if (head.data < curr.data)
{
Val = curr.data;
break ;
}
curr = curr.next;
} while (curr != head);
if (res == null )
{
res = new Node(Val);
tempList = res;
}
else
{
tempList.next = new Node(Val);
tempList = tempList.next;
}
head = head.next;
} while (head != H);
print(res);
}
static public void Main()
{
head = new Node(1);
head.next = new Node(5);
head.next.next = new Node(12);
head.next.next.next = new Node(10);
head.next.next.next.next = new Node(0);
head.next.next.next.next.next = head;
NextGreaterElement(head);
}
}
|
Javascript
<script>
let head;
class Node
{
constructor(data)
{
this .data = data;
this .next = null ;
}
}
function print(head)
{
let temp = head;
while (temp != null ) {
document.write(
temp.data + " " );
temp = temp.next;
}
}
function NextGreaterElement(head)
{
let H = head;
let res = null ;
let tempList = null ;
do {
let curr = head;
let Val = -1;
do {
if (head.data < curr.data) {
Val = curr.data;
break ;
}
curr = curr.next;
} while (curr != head);
if (res == null ) {
res = new Node(Val);
tempList = res;
}
else {
tempList.next = new Node(Val);
tempList = tempList.next;
}
head = head.next;
} while (head != H);
print(res);
}
head = new Node(1);
head.next = new Node(5);
head.next.next = new Node(12);
head.next.next.next = new Node(10);
head.next.next.next.next = new Node(0);
head.next.next.next.next.next = head;
NextGreaterElement(head);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Jun, 2022
Like Article
Save Article