Next Greater Element in a Circular Linked List
Last Updated :
01 Jun, 2022
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Node structure of the circular
// Linked list
struct Node
{
int data;
Node *next;
// Constructor
Node(int d)
{
data = d;
next = NULL;
}
};
// Function to print the elements
// of a Linked list
void print(Node *head)
{
Node *temp = head;
// Iterate the linked list
while (temp != NULL)
{
// Print the data
cout << temp->data << " ";
temp = temp->next;
}
}
// Function to find the next
// greater element for each node
void NextGreaterElement(Node *head)
{
// Stores the head of circular
// Linked list
Node *H = head;
// Stores the head of the
// resulting linked list
Node *res = NULL;
// Stores the temporary head of
// the resulting Linked list
Node *tempList = NULL;
// Iterate until head
// is not equal to H
do
{
// Used to iterate over the
// circular Linked List
Node *curr = head;
// Stores if there exist
// any next Greater element
int Val = -1;
// Iterate until head is
// not equal to curr
do
{
// If the current node
// is smaller
if (head->data < curr->data)
{
// Update the value
// of Val
Val = curr->data;
break;
}
// Update curr
curr = curr->next;
} while (curr != head);
// If res is Null
if (res == NULL)
{
// Create new Node with
// value curr->data
res = new Node(Val);
// Assign address of
// res to tempList
tempList = res;
}
else
{
// Update tempList
tempList->next = new Node(Val);
// Assign address of the
// next node to tempList
tempList = tempList->next;
}
// Update the value of
// head node
head = head->next;
} while (head != H);
// Print the resulting
// Linked list
print(res);
}
// Driver code
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;
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
static Node head;
// Node structure of the circular
// Linked list
static class Node {
int data;
Node next;
// Constructor
public Node(int data)
{
this.data = data;
this.next = null;
}
}
// Function to print the elements
// of a Linked list
static void print(Node head)
{
Node temp = head;
// Iterate the linked list
while (temp != null) {
// Print the data
System.out.print(
temp.data + " ");
temp = temp.next;
}
}
// Function to find the next
// greater element for each node
static void NextGreaterElement(Node head)
{
// Stores the head of circular
// Linked list
Node H = head;
// Stores the head of the
// resulting linked list
Node res = null;
// Stores the temporary head of
// the resulting Linked list
Node tempList = null;
// Iterate until head
// is not equal to H
do {
// Used to iterate over the
// circular Linked List
Node curr = head;
// Stores if there exist
// any next Greater element
int Val = -1;
// Iterate until head is
// not equal to curr
do {
// If the current node
// is smaller
if (head.data < curr.data) {
// Update the value
// of Val
Val = curr.data;
break;
}
// Update curr
curr = curr.next;
} while (curr != head);
// If res is Null
if (res == null) {
// Create new Node with
// value curr.data
res = new Node(Val);
// Assign address of
// res to tempList
tempList = res;
}
else {
// Update tempList
tempList.next = new Node(Val);
// Assign address of the
// next node to tempList
tempList = tempList.next;
}
// Update the value of
// head node
head = head.next;
} while (head != H);
// Print the resulting
// Linked list
print(res);
}
// Driver Code
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
# Python program for the above approach
# Node structure of the circular
# Linked list
class Node:
# Constructor
def __init__(self, data):
self.data = data
self.next = None
# Function to print the elements
# of a Linked list
def Print(head):
temp = head
# Iterate the linked list
while (temp != None):
# Print the data
print(temp.data,end=" ")
temp = temp.next
# Function to find the next
# greater element for each node
def NextGreaterElement(head):
# Stores the head of circular
# Linked list
H = head
# Stores the head of the
# resulting linked list
res = None
# Stores the temporary head of
# the resulting Linked list
tempList = None
# Iterate until head
# is not equal to H
while(True):
# Used to iterate over the
# circular Linked List
curr = head
# Stores if there exist
# any next Greater element
Val = -1
# Iterate until head is
# not equal to curr
# If the current node
# is smaller
while(True):
if (head.data < curr.data):
# Update the value
# of Val
Val = curr.data
break
# Update curr
curr = curr.next
if(curr == head):
break
# If res is Null
if (res == None):
# Create new Node with
# value curr.data
res = Node(Val)
# Assign address of
# res to tempList
tempList = res
else:
# Update tempList
tempList.next = Node(Val)
# Assign address of the
# next node to tempList
tempList = tempList.next
# Update the value of
# head node
head = head.next
if(head == H):
break
# Print the resulting
# Linked list
Print(res)
# Driver Code
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)
# This code is contributed by shinjanpatra
C#
// C# program for the above approach
using System;
// Node structure of the circular
// Linked list
class Node
{
public int data;
public Node next;
// Constructor
public Node(int data)
{
this.data = data;
this.next = null;
}
}
class GFG{
static Node head;
// Function to print the elements
// of a Linked list
static void print(Node head)
{
Node temp = head;
// Iterate the linked list
while (temp != null)
{
// Print the data
Console.Write(temp.data + " ");
temp = temp.next;
}
}
// Function to find the next
// greater element for each node
static void NextGreaterElement(Node head)
{
// Stores the head of circular
// Linked list
Node H = head;
// Stores the head of the
// resulting linked list
Node res = null;
// Stores the temporary head of
// the resulting Linked list
Node tempList = null;
// Iterate until head
// is not equal to H
do{
// Used to iterate over the
// circular Linked List
Node curr = head;
// Stores if there exist
// any next Greater element
int Val = -1;
// Iterate until head is
// not equal to curr
do{
// If the current node
// is smaller
if (head.data < curr.data)
{
// Update the value
// of Val
Val = curr.data;
break;
}
// Update curr
curr = curr.next;
} while (curr != head);
// If res is Null
if (res == null)
{
// Create new Node with
// value curr.data
res = new Node(Val);
// Assign address of
// res to tempList
tempList = res;
}
else
{
// Update tempList
tempList.next = new Node(Val);
// Assign address of the
// next node to tempList
tempList = tempList.next;
}
// Update the value of
// head node
head = head.next;
} while (head != H);
// Print the resulting
// Linked list
print(res);
}
// Driver Code
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);
}
}
// This code is contributed by unknown2108
JavaScript
<script>
// Javascript program for the above approach
let head;
// Node structure of the circular
// Linked list
class Node
{
// Constructor
constructor(data)
{
this.data = data;
this.next = null;
}
}
// Function to print the elements
// of a Linked list
function print(head)
{
let temp = head;
// Iterate the linked list
while (temp != null) {
// Print the data
document.write(
temp.data + " ");
temp = temp.next;
}
}
// Function to find the next
// greater element for each node
function NextGreaterElement(head)
{
// Stores the head of circular
// Linked list
let H = head;
// Stores the head of the
// resulting linked list
let res = null;
// Stores the temporary head of
// the resulting Linked list
let tempList = null;
// Iterate until head
// is not equal to H
do {
// Used to iterate over the
// circular Linked List
let curr = head;
// Stores if there exist
// any next Greater element
let Val = -1;
// Iterate until head is
// not equal to curr
do {
// If the current node
// is smaller
if (head.data < curr.data) {
// Update the value
// of Val
Val = curr.data;
break;
}
// Update curr
curr = curr.next;
} while (curr != head);
// If res is Null
if (res == null) {
// Create new Node with
// value curr.data
res = new Node(Val);
// Assign address of
// res to tempList
tempList = res;
}
else {
// Update tempList
tempList.next = new Node(Val);
// Assign address of the
// next node to tempList
tempList = tempList.next;
}
// Update the value of
// head node
head = head.next;
} while (head != H);
// Print the resulting
// Linked list
print(res);
}
// Driver Code
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);
// This code is contributed by unknown2108
</script>
Time Complexity: O(N2)
Auxiliary Space: O(N)
Similar Reads
Next greater element in the Linked List Given a linked list L of integers, the task is to return a linked list of integers such that it contains next greater element for each element in the given linked list. If there doesn't any greater element for any element then insert 0 for it. Examples: Input: 2->1->3->0->5 Output: 3-
15+ min read
Search an Element in Doubly Circular Linked List Pre-requisite: Convert an Array to a Circular Doubly Linked List, Doubly Circular Linked ListGiven a doubly circular linked list. The task is to find the position of an element in the list. Image Representation: Algorithm: Declare a temp pointer, and initialize it to the head of the list.Iterate the
13 min read
Count nodes in Circular linked list Given a circular linked list. The task is to find the length of the linked list, where length is defined as the number of nodes in the linked list.count nodes in the circular linked list.Using linear traversal - O(n) time and O(1) spaceThe idea is to start from the head node and traversing the list
5 min read
Delete all the even nodes of a Circular Linked List Given a circular singly linked list containing N nodes, the task is to delete all the even nodes from the list. Examples: Input : 57->11->2->56->12->61 Output : List after deletion : 57 -> 11 -> 61 Input : 9->11->32->6->13->20 Output : List after deletion : 9 -
10 min read
Introduction to Circular Linked List A circular linked list is a data structure where the last node connects back to the first, forming a loop. This structure allows for continuous traversal without any interruptions. Circular linked lists are especially helpful for tasks like scheduling and managing playlists, allowing for smooth navi
15+ min read
Insertion at the end in circular linked list A circular linked list is a data structure where each node points to the next, and the last node connects back to the first, creating a loop. Insertion at the end in circular linked list is an important operation. Understanding how to perform this insertion is essential for effectively manage and us
7 min read
Circular Linked List Implementation of Circular Queue The task is to implement the circular queue with the following operations using a circular linked list.Operations on Circular Queue:Front: Get the front item from the queue.Rear: Get the last item from the queue.enQueue(value): This function is used to insert an element into the circular queue. In a
10 min read
Deletion from a Circular Linked List In this article, we will learn how to delete a node from a circular linked list. In a circular linked list, the last node connects back to the first node, creating a loop.There are three main ways to delete a node from circular linked list:Deletion at the beginningDeletion at specific positionDeleti
15+ min read
Delete every Kth node from circular linked list Delete every kth node from a circular linked list until only one node is left. Also, print the intermediate lists. Examples: Input : n=4, k=2, list = 1->2->3->4 Output : 1->2->3->4->1 1->2->4->1 2->4->2 2->2 Input : n=9, k=4, list = 1->2->3->4->5-
13 min read
Deletion at different positions in a Circular Linked List tGiven a Circular Linked List. The task is to write programs to delete nodes from this list present at: First position.Last Position.At any given position i                                                      .Deleting first node from Singly Circular Linked List Examples:  Input : 99->11->2
15+ min read