Given a linked list of size N containing all values from 1 to N. The task is to sort the linked list in increasing order.
Examples:
Input : List = 3 -> 5 -> 4 -> 6 -> 1 -> 2
Output : 1 -> 2 -> 3 -> 4 -> 5 -> 6
Input : List = 5 -> 4 -> 3 -> 2 -> 1
Output : 1 -> 2 -> 3 -> 4 -> 5
Naive approach: The simplest approach is to sort this linked list with the use of any type of sorting method. It takes O(N*logN) minimum time.
Efficient approach: An efficient approach is to observe that the linked list contains a total of N elements and elements are numbered from 1 to N. Traverse the linked list and replace every element with its position.
Below is the implementation of this approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
bool sortList( struct Node* head)
{
int startVal = 1;
while (head != NULL) {
head->data = startVal;
startVal++;
head = head->next;
}
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node* node)
{
while (node != NULL) {
cout << node->data << " " ;
node = node->next;
}
}
int main()
{
struct Node* start = NULL;
push(&start, 2);
push(&start, 1);
push(&start, 6);
push(&start, 4);
push(&start, 5);
push(&start, 3);
sortList(start);
printList(start);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node start;
static void sortList(Node head)
{
int startVal = 1 ;
while (head != null )
{
head.data = startVal;
startVal++;
head = head.next;
}
}
static void push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
start = head_ref;
}
static void printList(Node node)
{
while (node != null )
{
System.out.print(node.data + " " );
node = node.next;
}
}
public static void main(String[] args)
{
start = null ;
push(start, 2 );
push(start, 1 );
push(start, 6 );
push(start, 4 );
push(start, 5 );
push(start, 3 );
sortList(start);
printList(start);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def sortList(head):
startVal = 1
while (head ! = None ):
head.data = startVal
startVal = startVal + 1
head = head. next
def push(head_ref, new_data):
new_node = Node(new_data)
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList(node):
while (node ! = None ):
print (node.data, end = " " )
node = node. next
if __name__ = = '__main__' :
head = None
head = push(head, 2 )
head = push(head, 1 )
head = push(head, 6 )
head = push(head, 4 )
head = push(head, 5 )
head = push(head, 3 )
sortList(head)
printList(head)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node start;
static void sortList(Node head)
{
int startVal = 1;
while (head != null )
{
head.data = startVal;
startVal++;
head = head.next;
}
}
static void push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
start = head_ref;
}
static void printList(Node node)
{
while (node != null )
{
Console.Write(node.data + " " );
node = node.next;
}
}
public static void Main(String[] args)
{
start = null ;
push(start, 2);
push(start, 1);
push(start, 6);
push(start, 4);
push(start, 5);
push(start, 3);
sortList(start);
printList(start);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
var start = null ;
function sortList(head) {
var startVal = 1;
while (head != null ) {
head.data = startVal;
startVal++;
head = head.next;
}
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
start = head_ref;
}
function printList(node) {
while (node != null ) {
document.write(node.data + " " );
node = node.next;
}
}
start = null ;
push(start, 2);
push(start, 1);
push(start, 6);
push(start, 4);
push(start, 5);
push(start, 3);
sortList(start);
printList(start);
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)