Sort list leetcode

Insertion Sort for Singly Linked List

We have discussed Insertion Sort for arrays. In this article, we are going to discuss Insertion Sort for a linked list.
Below is a simple insertion sort algorithm for a linked list.

1] Create an empty sorted [or result] list 2] Traverse the given list, do following for every node. ......a] Insert current node in sorted way in sorted or result list. 3] Change head of given linked list to head of sorted [or result] list.

Recommended: Please try your approach on {IDE} first, before moving on to the solution

The main step is [2.a] which has been covered in the below post.
Sorted Insert for Singly Linked List

Below is the implementation of the above algorithm

C




// C program to sort link list

// using insertion sort

#include

#include

struct node {

int data;

struct node* next;

};

struct node* head = NULL;

struct node* sorted = NULL;

void push[int val]

{

/* allocate node */

struct node* newnode

= [struct node*]malloc[sizeof[struct node]];

newnode->data = val;

/* link the old list off the new node */

newnode->next = head;

/* move the head to point to the new node */

head = newnode;

}

/*

* function to insert a new_node in a list. Note that

* this function expects a pointer to head_ref as this

* can modify the head of the input linked list

* [similar to push[]]

*/

void sortedInsert[struct node* newnode]

{

/* Special case for the head end */

if [sorted == NULL || sorted->data >= newnode->data] {

newnode->next = sorted;

sorted = newnode;

}

else {

struct node* current = sorted;

/* Locate the node before the point of insertion

*/

while [current->next != NULL

&& current->next->data < newnode->data] {

current = current->next;

}

newnode->next = current->next;

current->next = newnode;

}

}

// function to sort a singly linked list

// using insertion sort

void insertionsort[]

{

struct node* current = head;

// Traverse the given linked list and insert every

// node to sorted

while [current != NULL] {

// Store next for next iteration

struct node* next = current->next;

// insert current in sorted linked list

sortedInsert[current];

// Update current

current = next;

}

// Update head to point to sorted linked list

head = sorted;

}

/* Function to print linked list */

void printlist[struct node* head]

{

while [head != NULL] {

printf["%d->", head->data];

head = head->next;

}

printf["NULL"];

}

// Driver program to test above functions

int main[]

{

push[5];

push[20];

push[4];

push[3];

push[30];

printf["Linked List before sorting:\n"];

printlist[head];

printf["\n"];

insertionsort[head];

printf["Linked List after sorting:\n"];

printlist[head];

}

// This code is contributed by Sornodeep Chandra

C++




// C++ program to sort link list

// using insertion sort

#include

using namespace std;

struct Node {

int val;

struct Node* next;

Node[int x]

{

val = x;

next = NULL;

}

};

class LinkedlistIS {

public:

Node* head;

Node* sorted;

void push[int val]

{

/* allocate node */

Node* newnode = new Node[val];

/* link the old list off the new node */

newnode->next = head;

/* move the head to point to the new node */

head = newnode;

}

// function to sort a singly linked list using insertion

// sort

void insertionSort[Node* headref]

{

// Initialize sorted linked list

sorted = NULL;

Node* current = headref;

// Traverse the given linked list and insert every

// node to sorted

while [current != NULL] {

// Store next for next iteration

Node* next = current->next;

// insert current in sorted linked list

sortedInsert[current];

// Update current

current = next;

}

// Update head_ref to point to sorted linked list

head = sorted;

}

/*

* function to insert a new_node in a list. Note that

* this function expects a pointer to head_ref as this

* can modify the head of the input linked list

* [similar to push[]]

*/

void sortedInsert[Node* newnode]

{

/* Special case for the head end */

if [sorted == NULL || sorted->val >= newnode->val] {

newnode->next = sorted;

sorted = newnode;

}

else {

Node* current = sorted;

/* Locate the node before the point of insertion

*/

while [current->next != NULL

&& current->next->val < newnode->val] {

current = current->next;

}

newnode->next = current->next;

current->next = newnode;

}

}

/* Function to print linked list */

void printlist[Node* head]

{

while [head != NULL] {

cout val next;

}

}

};

// Driver program to test above functions

int main[]

{

LinkedlistIS list;

list.head = NULL;

list.push[5];

list.push[20];

list.push[4];

list.push[3];

list.push[30];

cout

Chủ Đề