Skip to content

Instantly share code, notes, and snippets.

@YonLiud
Created January 9, 2021 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YonLiud/57d1cf64a81fad9e78ad09597ecb447f to your computer and use it in GitHub Desktop.
Save YonLiud/57d1cf64a81fad9e78ad09597ecb447f to your computer and use it in GitHub Desktop.
Sort Node<T> LinkedList, Merge Sort
static Node<int> SortInput()
{
Console.WriteLine("Insert Value | Insert '-999' to stop");
int value = int.Parse(Console.ReadLine());
Node<int> node = new Node<int>(value);
while (value != -999)
{
node.SetNext(new Node<int>(value));
Console.WriteLine("Insert Value | Insert '-999' to stop");
value = int.Parse(Console.ReadLine());
node = node.GetNext();
}
return Sort(node);
}
static Node<int> Sort(Node<int> node)
{
while(node.HasNext())
{
Node<int> temp;
if(node.GetValue() >= node.GetNext().GetValue())
{
temp = node;
node = node.GetNext();
node.SetNext(temp);
}
node = node.GetNext();
}
return node;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment