Saturday, November 16, 2024
HomeSoftware DevelopmentLeft rotate Linked Checklist by X in teams of Y nodes

Left rotate Linked Checklist by X in teams of Y nodes

[ad_1]

  

utilizing System;

  

public class LinkedList {

    Node head;

  

    

    public class Node {

        public int knowledge;

        public Node subsequent;

        public Node(int d)

        {

            knowledge = d;

            subsequent = null;

        }

    }

  

    

    

    Node leftRotate(int x, int y)

    {

        Node prev = reverse(head, x,

                            y - x, true);

        return reverse(prev, y, y, true);

    }

  

    

    

    Node reverse(Node head, int m, int n,

                 bool isfirstHalf)

    {

        if (head == null)

            return null;

  

        Node present = head;

        Node subsequent = null;

        Node prev = null;

  

        int depend = 0, ok = m;

        if (!isfirstHalf)

            ok = n;

  

        

        whereas (depend < ok && present != null) {

            subsequent = present.subsequent;

            present.subsequent = prev;

            prev = present;

            present = subsequent;

            depend++;

        }

  

        

        

        

        

        

        

        if (subsequent != null)

            head.subsequent

                = reverse(subsequent, m, n,

                          !isfirstHalf);

  

        

        return prev;

    }

  

    

    

    void push(int new_data)

    {

        Node new_node = new Node(new_data);

        new_node.subsequent = head;

        head = new_node;

    }

  

    

    void printList()

    {

        Node temp = head;

        whereas (temp != null) {

            Console.Write(temp.knowledge + " ");

            temp = temp.subsequent;

        }

        Console.WriteLine();

    }

  

    

    public static void Foremost()

    {

        LinkedList llist = new LinkedList();

  

        

        

        

        for (int i = 100; i >= 10; i -= 10)

            llist.push(i);

  

        Console.WriteLine("Given checklist");

        llist.printList();

  

        llist.head = llist.leftRotate(2, 4);

  

        Console.WriteLine("Rotated Linked Checklist");

        llist.printList();

    }

}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments