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;
// A linked list node struct Node {
int data;
struct Node* next;
}; // Function to sort linked list void sortList( struct Node* head) {
struct Node* current = head;
struct Node* index = nullptr;
int temp;
while (current != nullptr) {
// Pointer to traverse the list from the current node
index = current->next;
while (index != nullptr) {
// If the current node's data is greater than the index node's data, swap them
if (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
} // Function to add a node at the beginning of Linked List void push( struct Node** head_ref, int new_data) {
struct Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
} // This function prints contents of linked list starting from the given node void printList( struct Node* node) {
while (node != nullptr) {
cout << node->data << " " ;
node = node->next;
}
} // Driver program to test above function int main() {
struct Node* start = nullptr;
// The constructed linked list is: 3->5->4->6->1->2
push(&start, 2);
push(&start, 1);
push(&start, 6);
push(&start, 4);
push(&start, 5);
push(&start, 3);
sortList(start);
cout << "Sorted Linked List: " ;
printList(start);
return 0;
} |
Java
import java.util.*;
class GFG {
static class Node {
int data;
Node next;
}
static Node start;
static Node sortList(Node head) {
Node current, index = null ;
int temp;
if (head == null ) {
return null ;
} else {
for (current = head; current.next != null ; current = current.next) {
for (index = current.next; index != null ; index = index.next) {
if (current.data > index.data) {
// Swap data between current and index nodes
temp = current.data;
current.data = index.data;
index.data = temp;
}
}
}
}
return head;
}
static Node 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;
return 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 ;
// The constructed linked list is: 3->5->4->6->1->2
start = push(start, 2 );
start = push(start, 1 );
start = push(start, 6 );
start = push(start, 4 );
start = push(start, 5 );
start = push(start, 3 );
start = sortList(start);
printList(start);
}
} |
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
# Function to sort linked list def sort_list(head):
current = head
while current is not None :
# Pointer to traverse the list from the current node
index = current. next
while index is not None :
# If the current node's data is greater than the index node's data, swap them
if current.data > index.data:
temp = current.data
current.data = index.data
index.data = temp
index = index. next
current = current. next
# Function to add a node at the beginning of Linked List def push(head_ref, new_data):
new_node = Node()
new_node.data = new_data
new_node. next = head_ref
return new_node
# This function prints contents of linked list starting from the given node def print_list(node):
while node is not None :
print (node.data, end = " " )
node = node. next
# Driver program to test above function if __name__ = = "__main__" :
start = None
# The constructed linked list is: 3->5->4->6->1->2
start = push(start, 2 )
start = push(start, 1 )
start = push(start, 6 )
start = push(start, 4 )
start = push(start, 5 )
start = push(start, 3 )
sort_list(start)
print ( "Sorted Linked List:" , end = " " )
print_list(start)
|
C#
using System;
// A linked list node public class Node
{ public int data;
public Node next;
} public class Program
{ // Function to sort linked list
public static void SortList(Node head)
{
Node current = head;
Node index = null ;
int temp;
while (current != null )
{
// Pointer to traverse the list from the current node
index = current.next;
while (index != null )
{
// If the current node's data is greater than the index node's data, swap them
if (current.data > index.data)
{
temp = current.data;
current.data = index.data;
index.data = temp;
}
index = index.next;
}
current = current.next;
}
}
// Function to add a node at the beginning of Linked List
public static Node Push(Node headRef, int newData)
{
Node newNode = new Node();
newNode.data = newData;
newNode.next = headRef;
return newNode;
}
// This function prints contents of linked list starting from the given node
public static void PrintList(Node node)
{
while (node != null )
{
Console.Write(node.data + " " );
node = node.next;
}
}
// Driver program to test above function
public static void Main()
{
Node start = null ;
// The constructed linked list is: 3->5->4->6->1->2
start = Push(start, 9);
start = Push(start, 1);
start = Push(start, 6);
start = Push(start, 4);
start = Push(start, 5);
start = Push(start, 3);
SortList(start);
Console.Write( "Sorted Linked List: " );
PrintList(start);
}
} |
Javascript
// A linked list node class Node { constructor() {
this .data = 0;
this .next = null ;
}
} // Function to sort linked list function sortList(head) {
let current = head;
let index = null ;
let temp;
while (current !== null ) {
// Pointer to traverse the list from the current node
index = current.next;
while (index !== null ) {
// If the current node's data is greater than the index node's data, swap them
if (current.data > index.data) {
temp = current.data;
current.data = index.data;
index.data = temp;
}
index = index.next;
}
current = current.next;
}
} // Function to add a node at the beginning of Linked List function push(headRef, newData) {
const newNode = new Node();
newNode.data = newData;
newNode.next = headRef;
return newNode;
} // This function prints contents of linked list starting from the given node function printList(node) {
while (node !== null ) {
console.log(node.data + " " );
node = node.next;
}
} // Driver program to test above function let start = null ;
// The constructed linked list is: 3->5->4->6->1->2 start = push(start, 2); start = push(start, 1); start = push(start, 6); start = push(start, 4); start = push(start, 5); start = push(start, 3); sortList(start); console.log( "Sorted Linked List: " );
printList(start); |
Output
1 2 3 4 5 6
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Recommended Articles