# Move Largest Number to the End of Linked List.

For a given linked list, you are supposed to write a function that moves the largest number to the end of the list.

Example 1:
Input : 4 6 2 8 1
Output : 4 6 2 1 8

Example 2:
Input : 9 5 1 8 2
Output : 5 1 8 2 9

Steps:

• To move the largest number to the end, first you need to write the function that returns the largest number. Store that largest(maximum) number in an integer variable `max`.

• Write the base case :

1. If only one node is present in the linked list , then return `head`.
2. If no node, then print an error message.
• Initialise two pointers : `prevnode` to point the previous node in the list and `temp` to point the `head` node of the list. Both the pointers are of type `struct node`.

• Compare the value of `max` with each number(data) of the linked list.

• If the value of `max` is equal to the first data of the linked list :

1. Separate the `head` node from the list, make `temp` to point second node and make `prevnode` to point `head` node.
2. Now, make second node as the `head` node of the list by `head = temp`.
3. Traverse the linked list until last node and connect the detached node(to which pointer `prevnode` is pointing) to the last node which contains the largest number of the linked list.
• If the value of `max` equals to other than first data in the list :
1. Traverse the linked list until number is found equal to value of `max` . If found, then detach that node from the linked list by `prevnode->next = temp->next`.
2. Create a new node pointing to by pointer `newnode` and store the value of `max` into that node using structure member `data` and store `NULL` into its address part using structure member `next` i.e., ```newnode->data = max; newnode->next = NULL;```
3. Traverse the linked list until last node and connect the `newnode` to the last node which contains the largest number of the linked list.
• Finally display the new linked list with the largest number at the end.

C program that moves the largest number to the end of linked list.

``````#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

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

{
struct node * temp;
while(temp!=0)
{
printf("%d ",temp->data);
temp = temp->next;
}
}

{
int max;
max = INT_MIN;

while(temp != NULL)
{
if(max < temp->data)
{
max = temp->data;
}
temp = temp->next;
}
return max;
}

{
struct node *prevnode, *newnode;
struct node *temp;
int max;

{
printf("\n--------------------------------\n");
printf("Linked list with largest number at end : ");
exit(0);
}

{
temp = temp->next;
while(temp->next != NULL )
{
temp = temp->next;
}
temp->next = prevnode;
printf("\n--------------------------------\n");
printf("Linked list with largest number at end : ");
exit(0);
}
while(temp->data != max)
{
prevnode = temp;
temp = temp->next;
}

prevnode->next = temp->next;

newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = max;
newnode->next = NULL;

while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;

printf("\n--------------------------------\n");
printf("Linked list with largest number at end : ");
}

int main()
{
struct node *head = 0, *newnode, *temp;
int n, choice, newdata, max;

printf("Enter the number of nodes in the list : ");
scanf("%d", &n);

if(n == 0)
{
printf("--------------------------------\n");
exit(0);
}
for(int i = 1; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the data%d : ", i);
scanf("%d", &newnode->data);
newnode->next = 0;
{
}
else
{
temp->next = newnode;
temp = newnode;
}
}
printf("--------------------------------\n");