Quick actions

cmd+k|ctrl+k

Navigation

Languages

ReverseLinkedList

Snippet info

Language

Csharp

Visibility

public

Author

anna1.1

Created

2020-07-15T08:50:46Z

Updated

2020-07-17T08:24:11Z

using System;
using System.Collections.Generic;

public class MainClass
{
    static void Main(string[] args)
    {
        // create list
        var list = new LinkedList<int>(new[] { 1, 2, 3, 4 });

        // reverse list
        var n = list.First; // start from first which will remain constant in the list
        while (n.Next != null) // at the end current will point to null
        {
            var current = n.Next; // store next n
            list.Remove(current); // remove next n after current 
            list.AddFirst(current.Value); // place next value at the start
        }

        // display reversed list
        foreach (int node in list)
        {
            Console.WriteLine($"{node}");
        }
    }
}
INFO