Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Assignment #1
- public class Solver
- {
- public static void main(String args[])
- {
- LinkedList list = new LinkedList();
- list.insertFirst(new Node(1));
- list.insertFirst(new Node(2));
- list.insertFirst(new Node(3));
- list.insertFirst(new Node(4));
- System.out.print("\nThe List before Rotating Left: ");
- list.print();
- rotateLeft(list);
- System.out.print("The List after Rotating Left: ");
- list.print();
- System.out.println();
- }
- public static void rotateLeft(LinkedList list)
- {
- if (list.isEmpty())
- {
- System.out.println("The List is Empty");
- return;
- }
- if (list.First.next != null)
- {
- Node curr = list.First;
- while (curr.next != null)
- {
- curr = curr.next;
- }
- curr.next = list.First;
- list.First = list.First.next;
- curr.next.next = null;
- }
- System.out.println("-----\nDone, The List is rotated left\n-----");
- }
- }
- class Node
- {
- int data;
- Node next;
- public Node(int d)
- {
- data = d;
- }
- }
- class LinkedList
- {
- Node First;
- void insertFirst(Node newNode)
- {
- newNode.next = First;
- First = newNode;
- // System.out.println("Done, Node is inserted at First");
- }
- Node deleteFirst()
- {
- if (isEmpty())
- {
- System.out.println("The List is Empty");
- return null;
- }
- Node temp = First;
- First = First.next;
- temp.next = null;
- // System.out.println("Done, First Node is deleted");
- return temp;
- }
- void print()
- {
- if (isEmpty())
- {
- System.out.println("The List is Empty");
- return;
- }
- Node curr = First;
- while (curr != null)
- {
- System.out.printf("%d ", curr.data);
- curr = curr.next;
- }
- System.out.println();
- }
- boolean isEmpty()
- {
- return First == null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment