Software & Finance





C# - Reverse Singly Linked List





I have given here C# sample code to reverse a singly linked list.

 

Reversing a singly linked list is very easy. The SLinkList Reverse(SLinkList head) function is about ten lines of code that does the magic.

 

Singly Linked List, Doubly Linked List, Singly Linked Circular List and Doubly Linked Circular List are an important data structure used in C# applications.

Related Links:


 

Source Code


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace CSTest

{

 

   class SLinkList

   {

      private int data;

      private SLinkList next;

 

      public SLinkList()

      {

         data = 0;

         next = null;

      }

 

      public SLinkList(int value)

      {

         data = value;

         next = null;

      }

 

 

      public SLinkList InsertNext(int value)

      {

         SLinkList node = new SLinkList(value);

         if(this.next == null)

         {

               // Easy to handle

               node.next = null; // already set in constructor

               this.next = node;

         }

         else

         {

               // Insert in the middle

               SLinkList temp = this.next;

               node.next = temp;

               this.next = node;

          }

          return node;

      }

 

      public int DeleteNext()

      {

         if(next == null)

               return 0;

 

          SLinkList node = this.next;

          this.next = this.next.next// can be NULL here

          node = null;

          return 1;

      }

 

      public void Traverse(SLinkList node)

      {

         if(node == null)

               node = this;

         System.Console.WriteLine("\n\nTraversing in Forward Direction\n\n");

 

         while(node != null)

         {

            System.Console.WriteLine(node.data);

               node = node.next;

         }

      }

 

public SLinkList Reverse_With_Bubble(SLinkList head)

      {

         SLinkList testnode = null;

         if (head == null)

            return head;

 

         while (true)

         {

            SLinkList p = head;

            if (p.next == testnode)

               break;

            while (true)

            {

               int temp = p.data;

               p.data = p.next.data;

               p.next.data = temp;

               p = p.next;

               if (p.next == testnode)

               {

                  testnode = p;

                  break;

               }

            }

         }

         return head;

      }

 

      public SLinkList Reverse(SLinkList head)

      {

         if (head == null)

            return head;

 

         SLinkList cp = head;

         SLinkList prev = null;

 

         while (cp != null)

         {

            SLinkList next = cp.next;

            cp.next = prev;

            prev = cp;

            cp = next;

         }

         head = prev;

         return head;

      }

 

      public SLinkList Reverse_with_copy(SLinkList head)

      {

            SLinkList p = head;

            SLinkList newhead = null;

            while(true)

            {

                  if(p == null)

                        return newhead;

                  SLinkList newnode = new SLinkList(p.data);

                  newnode.next = newhead;

                  newhead = newnode;

                  p = p.next;

            }

      }

   }

 

   class Program

   {

      static void Main(string[] args)

      {

         SLinkList head = new SLinkList(1);

         SLinkList p = head.InsertNext(2);

         p = p.InsertNext(3);

         p = p.InsertNext(4);

         p = p.InsertNext(5);

         p = p.InsertNext(6);

         p = p.InsertNext(7);

         p = p.InsertNext(8);

 

         head.Traverse(null);

 

         SLinkList newhead = head.Reverse(head);     

         newhead.Traverse(null);

      }

   }

}

 

Output


 

Traversing in Forward Direction

1
2
3
4
5

6

7

8

 

Traversing in Forward Direction (After Reversal)

8
7

6

5

4

3

2

1