Sunday, 17 September 2017

How do you reverse a singly linked list

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestAlgo
{
    class Program
    {
        static void Main(string[] args)
        {
            LinkedLst objLst = new LinkedLst();
            objLst.AddItem(1);

            objLst.AddItem(2);
            objLst.AddItem(3);
            objLst.AddItem(4);
            objLst.AddItem(5);
            objLst.AddItem(6);

            objLst.PrintLLinkedList(objLst);
            objLst.ReveseLinkedLst(objLst);
}
}


-------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestAlgo
{
  public  class LinkedLst
    {
      Node head;
      public class Node
      {
        public int data;
        public Node nxt;
        public Node(int d)
        {
            this.data = d;
            this.nxt = null;
        }
        public Node()
        { }

      }


      public void GetNthNode(LinkedLst lst,int n)
      {
          Node mainPoint = lst.head;
          Node refPoint = lst.head;

          int count = 0;
          while (count < n)
          {
              if (refPoint == null)
                  break;

              refPoint = refPoint.nxt;
              count++;
          }

          while (refPoint != null)
          {
              mainPoint = mainPoint.nxt;
              refPoint = refPoint.nxt;
          }

          Console.WriteLine(n+ "th element from last in linked list= " + mainPoint.data );
      }


      public void AddItem(int d)
      {
          Node newNode = new Node(d);
          newNode.nxt = head;
          head = newNode;

      }

      public void PrintMiddle(LinkedLst lst)
      {

          Node slow = lst.head;
          Node fast = lst.head;

          while (slow != null && fast.nxt!= null)
          {
              slow = slow.nxt;
              
              fast = fast.nxt.nxt;
              if (fast == null)
                  break;

          }

          Console.WriteLine(slow.data);

          //List<int> lstObj = new List<int>();
          
          //Node newNode = lst.head;
          //while (newNode != null)
          //{
          //    lstObj.Add( newNode.data);
          //    newNode = newNode.nxt;
             
          //}
          
          //int middleindex;
          //if (lstObj.Count() == 1)
          //    middleindex = 0;
          //else
          //    middleindex = lstObj.Count() / 2;
          //Console.WriteLine(lstObj.ElementAt(middleindex));

      }

      public void PrintLLinkedList(LinkedLst lst)
      {
          Node newNOde = lst.head;

          while (newNOde != null)
          {
              Console.WriteLine(newNOde.data);
              newNOde = newNOde.nxt;
          }
         // Console.WriteLine(lst.head.data);
      }

      public void ReveseLinkedLst(LinkedLst lst)
      {
          Node reversed =new Node();
          LinkedLst newlst =new LinkedLst();
          Node original = lst.head;

          while (original != null)
          {
              reversed = new Node(original.data);
             
              reversed.nxt = newlst.head;
              newlst.head = reversed;
              original = original.nxt;
          }

          Console.WriteLine("Revesed linked list");
          Node newN = newlst.head;
          while (newN != null)
          {
              
              Console.WriteLine( newN.data);
              newN = newN.nxt;

          }
      }


    }
}

No comments:

Post a Comment