# 按照数组中出现元素的顺序对链表进行排序

2021年4月30日19:16:42 发表评论 638 次浏览

arr [] =

list=

## C ++

``````//Efficient CPP program to sort given list in order
//elements are appearing in an array
#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
struct Node* next;
};

//function prototype for printing the list
void printList( struct Node*);

//Function to insert a node at the
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node -> data = new_data;
}

//function to print the linked list
{
cout <<head -> data <<" -> " ;
}
}

//Function that sort list in order of apperaing
//elements in an array
void sortlist( int arr[], int N, struct Node* head)
{
//Store frequencies of elements in a
//hash table.
unordered_map<int , int> hash;
while (temp) {
hash[temp -> data]++;
temp = temp -> next;
}

//One by one put elements in lis according
//to their appearance in array
for ( int i = 0; i <N; i++) {

//Update 'frequency' nodes with value
//equal to arr[i]
int frequency = hash[arr[i]];
while (frequency--) {

//Modify list data as element
//appear in an array
temp -> data = arr[i];
temp = temp -> next;
}
}
}

//Driver Code
int main()
{
int arr[] = { 5, 1, 3, 2, 8 };
int N = sizeof (arr) /sizeof (arr);

//Function call to sort the list in order
//elements are apperaing in an array

cout <<"Sorted List:" <<endl;
return 0;
}``````

## Java

``````//Efficient JAVA program to sort given list in order
//elements are appearing in an array
import java.util.*;

class GFG
{

static class Node
{
int data;
Node next;
};

//Function to insert a node at the
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
}

//function to print the linked list
{
{
}
}

//Function that sort list in order of apperaing
//elements in an array
static void sortlist( int arr[], int N, Node head)
{
//Store frequencies of elements in a
//hash table.
HashMap<Integer, Integer> hash = new HashMap<Integer, Integer>();
while (temp != null )
{
if (hash.containsKey(temp.data))
hash.put(temp.data, hash.get(temp.data) + 1 );
else
hash.put(temp.data, 1 );
temp = temp.next;
}

//One by one put elements in lis according
//to their appearance in array
for ( int i = 0 ; i <N; i++)
{

//Update 'frequency' nodes with value
//equal to arr[i]
int frequency = hash.get(arr[i]);
while (frequency--> 0 )
{

//Modify list data as element
//appear in an array
temp.data = arr[i];
temp = temp.next;
}
}
}

//Driver Code
public static void main(String[] args)
{
int arr[] = { 5 , 1 , 3 , 2 , 8 };
int N = arr.length;

//Function call to sort the list in order
//elements are apperaing in an array

System.out.print( "Sorted List:" + "\n" );
}
}

//This code is contributed by PrinciRaj1992``````

## Python3

``````# Efficient Python3 program to sort given list in order
# elements are appearing in an array

class Node:
def __init__( self ):
self .data = 0
self . next = None

# Function to insert a node at the
# beginning of the linked list

new_node = Node()
new_node.data = new_data

# function to print the linked list

while (head ! = None ):

print (head.data, end = "->" )

# Function that sort list in order of apperaing
# elements in an array

# Store frequencies of elements in a
# hash table.
hash = dict ()
while (temp ! = None ) :

hash [temp.data] = hash .get(temp.data, 0 ) + 1
temp = temp. next

# One by one put elements in lis according
# to their appearance in array
for i in range (N):

# Update 'frequency' nodes with value
# equal to arr[i]
frequency = hash .get(arr[i], 0 )

while (frequency> 0 ) :

frequency = frequency - 1
# Modify list data as element
# appear in an array
temp.data = arr[i]
temp = temp. next

# Driver Code

arr = [ 5 , 1 , 3 , 2 , 8 ]
N = len (arr)

# Function call to sort the list in order
# elements are apperaing in an array

# print the modified linked list
print ( "Sorted List:" )

# This code is contributed by Arnab Kundu``````

## C#

``````//Efficient C# program to sort given list in order
//elements are appearing in an array
using System;
using System.Collections.Generic;

class GFG
{

public class Node
{
public int data;
public Node next;
};

//Function to insert a node at the
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
}

//function to print the linked list
{
{
}
}

//Function that sort list in order of apperaing
//elements in an array
static void sortlist( int []arr, int N, Node head)
{
//Store frequencies of elements in a
//hash table.
Dictionary<int , int> hash = new Dictionary<int , int>();
while (temp != null )
{
if (hash.ContainsKey(temp.data))
hash[temp.data] = hash[temp.data] + 1;
else
temp = temp.next;
}

//One by one put elements in lis according
//to their appearance in array
for ( int i = 0; i <N; i++)
{

//Update 'frequency' nodes with value
//equal to arr[i]
int frequency = hash[arr[i]];
while (frequency-->0)
{

//Modify list data as element
//appear in an array
temp.data = arr[i];
temp = temp.next;
}
}
}

//Driver Code
public static void Main(String[] args)
{
int []arr = { 5, 1, 3, 2, 8 };
int N = arr.Length;

//Function call to sort the list in order
//elements are apperaing in an array

``````Sort list: 