Rearrange linked list in increasing order

Here given code implementation process.

``````//C Program
//Rearrange linked list in increasing order (Sort order list)
#include <stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node
{
int data;
struct Node*next;
};

//insert Node element
{
//Create dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL)
{
printf("Memory overflow\n");

}else
{
node->data=value;
node->next=NULL;
{

}
else
{
//find last node
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}
}
}

{

{
return;
}
else
{
//Finding location of inserting nodes
while(temp->next!=NULL && temp->next->data < element->data)
{
temp=temp->next;
}

element->next=temp->next;
temp->next=element;
}

}
{
{
return;
}
struct Node*next=temp->next;

{
//get first smallest element
}
temp->next=NULL;
//Recursively execute until the last node

{
//Add node to its proper position
}
}

//Display element of Node
void display(struct Node*temp)
{

if(temp==NULL)
{
}

while(temp!=NULL)
{
printf("%d  ",temp->data);
temp=temp->next;
}
}
int main()
{
//create node pointer

printf("Before Sort\n");
//display all node

printf("\nAfter Sort\n");
//display all node
return 0;
}``````

Output

``````Before Sort
3  5  2  1  6  4  -3
After Sort
-3  1  2  3  4  5  6``````
``````//C++ Program
//Rearrange linked list in increasing order
#include <iostream>
using namespace std;

//create structure
struct Node
{
int data;
struct Node*next;
};
{

public:
void insert(int);
void display();
void sortList(Node**,Node*);
void arrange(Node*,Node*);

};
{
}
//insert node at end of linked list
{
//Create dynamic node
struct Node*node=new Node;
if(node==NULL)
{
cout<<"Memory overflow\n";
}else
{
node->data=value;
node->next=NULL;
{
//base condition
}else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}
}
}
//display all node value in linked list
{
{
}
else
{
while(temp!=NULL)
{
//print node value
cout<<temp->data<<"  ";
temp=temp->next;
}
cout<<endl;
}
}
{

{
return;
}
else
{
//Finding location of inserting node
while(temp->next!=NULL && temp->next->data < element->data)
{
temp=temp->next;
}
element->next=temp->next;
temp->next=element;
}

}
{
{
return;
}
Node*next=temp->next;

{
//Get first smallest element
}
temp->next=NULL;
//Recursively execute until the last node

{
//Add node to its proper position
}
}

int main()
{

//create object

obj.insert(3);
obj.insert(5);
obj.insert(2);
obj.insert(1);
obj.insert(6);
obj.insert(4);
obj.insert(-3);

cout<<("Before Sort\n");
//display all node
obj.display();

cout<<("\nAfter Sort\n");
//display all node
obj.display();
return 0;
}``````

Output

``````Before Sort

Linked List : 3  5  2  1  6  4  -3

After Sort

Linked List : -3  1  2  3  4  5  6  ``````
``````//Java Program
//Rearrange linked list in increasing order
{

static class Node
{
int data;
Node next;
}
//Class constructors
{
back=null;
}
//insert element
public void insert(int value)
{
//Create  node
Node node=new Node();
node.data=value;
node.next=null;
{
}
else
{
//find last node
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=node;
}

}
public void display()
{
{
while(temp!=null)
{
System.out.print("  "+temp.data);
temp=temp.next;
}
System.out.println();
}else
{
}
}

public void arrange(Node element)
{

{
return;
}
else
{
//Finding location of inserting nodes
while(temp.next!=null && temp.next.data < element.data)
{
temp=temp.next;
}

element.next=temp.next;
temp.next=element;
}

}
public void sortList(Node temp)
{
{
return;
}
Node next=temp.next;

{
//Get first smallest element
}
temp.next=null;
//Recursively execute until the last node
sortList(next);

{
//Add node to its proper position
arrange(temp);
}
}
public static void main(String[] args)
{

obj.insert(3);
obj.insert(5);
obj.insert(2);
obj.insert(1);
obj.insert(6);
obj.insert(4);
obj.insert(-3);

System.out.print("Before Sort\n");
//display all node
obj.display();

System.out.print("\nAfter Sort\n");
//display all node
obj.display();

}
}``````

Output

``````Before Sort
3  5  2  1  6  4  -3

After Sort
-3  1  2  3  4  5  6
``````
``````#Python Program
#Rearrange linked list in increasing order
class Node:
def __init__(self,data):
self.data=data
self.next=None

def __init__(self):
#Assign default value

#insert new node to linked list
def insert(self,data):
node=Node(data)
node.next=None
else:
while temp.next!=None:
temp=temp.next
temp.next=node

def display(self):
return

while(temp!=None):
print(temp.data,end="  ")
temp=temp.next

print()

def arrange(self,element):

return;

else:
#Finding location of inserting nodes
while(temp.next!=None and temp.next.data < element.data):
temp=temp.next;

element.next=temp.next;
temp.next=element;

def sortList(self,temp):

return;

nextNode=temp.next;

#Get first smallest element

temp.next=None;
#Recursively execute until the last node
self.sortList(nextNode);

#Add node to its proper position
self.arrange(temp);

def main():

obj.insert(3);
obj.insert(5);
obj.insert(2);
obj.insert(1);
obj.insert(6);
obj.insert(4);
obj.insert(-3);

print("Before Sort\n");
#display all node
obj.display();

print("\nAfter Sort\n");
#display all node
obj.display();
if __name__=="__main__":
main()``````

Output

``````Before Sort

3  5  2  1  6  4  -3

After Sort

-3  1  2  3  4  5  6
``````
``````//C# Program
//Rearrange linked list in increasing order
using System;
public class Node
{
public int data;
public Node next;
}

{

//Class constructors
{

}
//insert element
public void insert(int value)
{
//Create  node
Node node=new Node();
node.data=value;
node.next=null;
{
}
else
{
//find last node
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=node;
}

}
public void display()
{
{
while(temp!=null)
{
Console.Write("  "+temp.data);
temp=temp.next;
}
Console.WriteLine();
}else
{
}
}

public void arrange(Node element)
{

{
return;
}
else
{
//Finding location of inserting nodes
while(temp.next!=null && temp.next.data < element.data)
{
temp=temp.next;
}

element.next=temp.next;
temp.next=element;
}

}
public void sortList(Node temp)
{
{
return;
}
Node next=temp.next;

{
//Get first smallest element
}
temp.next=null;
//Recursively execute until the last node
sortList(next);

{
//Add node to its proper position
arrange(temp);
}
}
public static void Main(String[] args)
{

obj.insert(3);
obj.insert(5);
obj.insert(2);
obj.insert(1);
obj.insert(6);
obj.insert(4);
obj.insert(-3);

Console.Write("Before Sort\n");
//display all node
obj.display();

Console.Write("\nAfter Sort\n");
//display all node
obj.display();

}
}``````

Output

``````Before Sort
3  5  2  1  6  4  -3

After Sort
-3  1  2  3  4  5  6
``````
``````<?php
//Php program
//Rearrange linked list in circular order
class Node
{
public \$data;
public \$next;
function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}
{

function __construct()
{
}
/*
* Append the Given data value at end of linked list
* Fun : insert
* Parm: data value
*@return None
*/
function insert(\$data)
{
\$newNode=new Node(\$data);
{
}else
{
//find last node of linked list
while(\$temp->next!=NULL)
{
\$temp=\$temp->next;
}
\$temp->next=\$newNode;
}
}
//Display all inserted node in linked list
function display()
{
{
}
else
{
while(\$temp!=NULL)
{
//display node value
echo "  ".\$temp->data;
\$temp=\$temp->next; //visit to next node
}
}
}

function arrange(\$element)
{

{
return;
}
else
{
//Finding location of inserting nodes
while(\$temp->next!=NULL && \$temp->next->data < \$element->data)
{
\$temp=\$temp->next;
}

\$element->next=\$temp->next;
\$temp->next=\$element;
}

}
function sortList(\$temp)
{
{
return;
}
\$next=\$temp->next;

{
//Get first smallest element
}
\$temp->next=NULL;
//Recursively execute until the last node
\$this->sortList(\$next);

{
//Add node to its proper position
\$this->arrange(\$temp);
}
}

}

function main()
{

//create object

\$obj->insert(3);
\$obj->insert(5);
\$obj->insert(2);
\$obj->insert(1);
\$obj->insert(6);
\$obj->insert(4);
\$obj->insert(-3);

echo ("Before Sort");
//display all node
\$obj->display();

echo ("\nAfter Sort");
//display all node
\$obj->display();

}
main();
?>``````

Output

``````Before Sort
Linked List :  3  5  2  1  6  4  -3
After Sort
Linked List :  -3  1  2  3  4  5  6``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.