📌  相关文章
📜  在链接列表中将每个节点替换为其“超越者计数”

📅  最后修改于: 2021-05-30 05:14:01             🧑  作者: Mango

给定LinkedList,将每个节点的值替换为其超越者计数。那是对它的权利更大的要素的数量。

例子:

简单方法

  1. 取两个指针px 。指针p用于遍历列表,而x用于遍历每个节点的列表的右半部分。
  2. 初始化变量计数以对大于当前节点的节点进行计数。
  3. 使用指针p遍历列表中的所有节点。
    • 将计数初始化为0。
    • 初始化指针x以指向当前节点p
    • 计算大于当前节点的节点数。
    • 用计数替换当前节点。
  4. 重复步骤4,直到列表完全遍历为止。

下面是上述方法的实现:

C++
// C++ program to replace the nodes
// with their surpasser count
  
#include 
using namespace std;
  
// A linked list node
struct Node {
    int data;
    struct Node* next;
};
  
// Utility function to create a new Node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->next = NULL;
  
    return temp;
}
  
// Function to display the linked list
void printList(Node* node)
{
    while (node != NULL) {
  
        cout << node->data << " ";
  
        node = node->next;
    }
}
  
// Function to check Surpasser Count
void replaceNodes(Node* head)
{
    // Pointer used to traverse through
    // all the nodes in the list
    Node* p = head;
  
    // Pointer used to traverse through the right
    // elements to count the greater elements
    Node* x = NULL;
  
    // Variable to count the number of
    // elements greater than the
    // current element on right
    int count = 0;
  
    int i;
  
    // Traverse through all the elements
    // in the list
    while (p != NULL) {
  
        count = 0;
  
        i = 0;
  
        // Initialize x to current node
        x = p;
  
        // Check or count the number of nodes
        // that are greater than the current
        // node on right
        while (x != NULL) {
  
            if (x->data > p->data)
                count++;
  
            x = x->next;
        }
  
        // Replace the node data with the
        // count of elements greater than
        // the current element
        p->data = count;
        p = p->next;
    }
}
  
// Driver code
int main()
{
    // Creating the linked list
    Node* head = newNode(10);
    head->next = newNode(12);
    head->next->next = newNode(5);
    head->next->next->next = newNode(40);
    head->next->next->next->next = newNode(21);
    head->next->next->next->next->next = newNode(70);
    head->next->next->next->next->next->next = newNode(1);
    head->next->next->next->next->next->next->next = newNode(49);
    head->next->next->next->next->next->next->next->next = newNode(37);
  
    replaceNodes(head);
  
    printList(head);
  
    return 0;
}


Java
// Java program to replace the nodes
// with their surpasser count
  
class GFG {
  
// A linked list node
static class Node {
    int data;
    Node next;
};
   
// Utility function to create a new Node
static Node newNode(int data)
{
    Node temp = new Node();
    temp.data = data;
    temp.next = null;
   
    return temp;
}
   
// Function to display the linked list
static void printList(Node node)
{
    while (node != null) {
        System.out.print(node.data+" ");
   
        node = node.next;
    }
}
   
// Function to check Surpasser Count
static void replaceNodes(Node head)
{
    // Pointer used to traverse through
    // all the nodes in the list
    Node p = head;
   
    // Pointer used to traverse through the right
    // elements to count the greater elements
    Node x = null;
   
    // Variable to count the number of
    // elements greater than the
    // current element on right
    int count = 0;
   
    int i;
   
    // Traverse through all the elements
    // in the list
    while (p != null) {
   
        count = 0;
   
        i = 0;
   
        // Initialize x to current node
        x = p;
   
        // Check or count the number of nodes
        // that are greater than the current
        // node on right
        while (x != null) {
   
            if (x.data > p.data)
                count++;
   
            x = x.next;
        }
   
        // Replace the node data with the
        // count of elements greater than
        // the current element
        p.data = count;
        p = p.next;
    }
}
   
// Driver code
public static void main(String[] args) {
  // Creating the linked list
    Node head = newNode(10);
    head.next = newNode(12);
    head.next.next = newNode(5);
    head.next.next.next = newNode(40);
    head.next.next.next.next = newNode(21);
    head.next.next.next.next.next = newNode(70);
    head.next.next.next.next.next.next = newNode(1);
    head.next.next.next.next.next.next.next = newNode(49);
    head.next.next.next.next.next.next.next.next = newNode(37);
   
    replaceNodes(head);
   
    printList(head);
    }
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python3 program to replace the nodes 
# with their surpasser count 
  
# A linked list node 
class Node:
      
    def __init__(self, data):
        self.data = data 
        self.next = None
  
# Function to display the linked list 
def printList(node): 
  
    while node != None: 
        print(node.data, end = " ") 
        node = node.next
      
# Function to check Surpasser Count 
def replaceNodes(head): 
  
    # Pointer used to traverse through 
    # all the nodes in the list 
    p = head 
  
    # Pointer used to traverse through
    # the right elements to count the 
    # greater elements 
    x = None
  
    # Variable to count the number of 
    # elements greater than the 
    # current element on right 
    count = 0
  
    # Traverse through all the elements 
    # in the list 
    while p != None: 
  
        count = 0
  
        # Initialize x to current node 
        x = p 
  
        # Check or count the number of nodes 
        # that are greater than the current 
        # node on right 
        while x != None: 
  
            if x.data > p.data: 
                count += 1
  
            x = x.next
  
        # Replace the node data with the 
        # count of elements greater than 
        # the current element 
        p.data = count 
        p = p.next
  
# Driver code 
if __name__ == "__main__":
  
    # Creating the linked list 
    head = Node(10) 
    head.next = Node(12) 
    head.next.next = Node(5) 
    head.next.next.next = Node(40) 
    head.next.next.next.next = Node(21) 
    head.next.next.next.next.next = Node(70) 
    head.next.next.next.next.next.next = Node(1) 
    head.next.next.next.next.next.next.next = Node(49) 
    head.next.next.next.next.next.next.next.next = Node(37) 
  
    replaceNodes(head) 
  
    printList(head) 
  
# This code is contributed by Rituraj Jain


C#
// C# program to replace the nodes
// with their surpasser count
using System;
  
class GFG 
{
  
// A linked list node
public class Node
{
    public int data;
    public Node next;
};
  
// Utility function to create a new Node
static Node newNode(int data)
{
    Node temp = new Node();
    temp.data = data;
    temp.next = null;
  
    return temp;
}
  
// Function to display the linked list
static void printList(Node node)
{
    while (node != null) 
    {
        Console.Write(node.data + " ");
  
        node = node.next;
    }
}
  
// Function to check Surpasser Count
static void replaceNodes(Node head)
{
    // Pointer used to traverse through
    // all the nodes in the list
    Node p = head;
  
    // Pointer used to traverse through the right
    // elements to count the greater elements
    Node x = null;
  
    // Variable to count the number of
    // elements greater than the
    // current element on right
    int count = 0;
  
    int i;
  
    // Traverse through all the elements
    // in the list
    while (p != null) 
    {
  
        count = 0;
  
        i = 0;
  
        // Initialize x to current node
        x = p;
  
        // Check or count the number of nodes
        // that are greater than the current
        // node on right
        while (x != null) 
        {
  
            if (x.data > p.data)
                count++;
  
            x = x.next;
        }
  
        // Replace the node data with the
        // count of elements greater than
        // the current element
        p.data = count;
        p = p.next;
    }
}
  
// Driver code
public static void Main() 
{
    // Creating the linked list
    Node head = newNode(10);
    head.next = newNode(12);
    head.next.next = newNode(5);
    head.next.next.next = newNode(40);
    head.next.next.next.next = newNode(21);
    head.next.next.next.next.next = newNode(70);
    head.next.next.next.next.next.next = newNode(1);
    head.next.next.next.next.next.next.next = newNode(49);
    head.next.next.next.next.next.next.next.next = newNode(37);
  
    replaceNodes(head);
  
    printList(head);
}
}
  
/* This code contributed by PrinciRaj1992 */


输出:
6 5 5 2 3 0 2 0 0

时间复杂度:O(N 2 ),其中N是链表中节点的数量。
辅助空间:O(1)

想要从精选的最佳视频中学习和练习问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”