📜  循环链表中的下一个更大元素

📅  最后修改于: 2022-05-13 01:56:10.220000             🧑  作者: Mango

循环链表中的下一个更大元素

给定一个循环单链表,任务是为链表中的每个节点打印下一个更大的元素。如果任何节点都没有下一个更大的元素,则为该节点打印“-1”

例子:

方法:给定的问题可以通过向右迭代循环链表直到相同的元素再次出现然后打印为每个节点找到的下一个更大的元素来解决。请按照以下步骤解决问题:

  • 初始化一个 Node 变量,比如res为 NULL 来存储链接列表的头节点,表示下一个更大的元素。
  • 此外,初始化一个 Node 变量,比如tempList为 NULL 以遍历表示下一个更大元素的链接列表。
  • 遍历循环链表并执行以下步骤:
    • 初始化一个节点,比如curr来存储对循环链表当前节点的引用。
    • 将变量Val初始化为-1以存储当前节点的下一个较大元素的值。
    • 执行以下步骤,直到curr不等于循环链表的当前节点的引用:
      • 如果当前节点curr的值大于循环链表的当前节点的值,则将 curr.data的值赋给Val并跳出循环。
      • curr节点的值更新为curr = curr.next
    • 如果节点resNULL则创建一个值为Val的新节点并将其分配给res ,然后将res的值分配给tempList
    • 否则,创建一个值为Val的新节点并将其分配给tempList.next ,然后将节点tempList更新为tempList = tempList.next。
  • 完成以上步骤后,打印出Linked List res的元素作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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


输出:
5 12 -1 12 1

时间复杂度: O(N 2 )
辅助空间: O(N)