m2skills

reverse starting ll java

Jan 30th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Created by MOHIT on 24-01-2018.
  5.  */
  6.  
  7. class node{
  8.  
  9.     public int element;
  10.     public node link;
  11.  
  12.     //constructor that accepts only element
  13.     node(int element) {
  14.         this.element = element;
  15.         this.link = null;
  16.     }
  17.  
  18.     //constructor that accepts both link and element
  19.     node(int element, node link){
  20.         this.element = element;
  21.         this.link = link;
  22.     }
  23.  
  24.     //method to update the element
  25.     void updateData(int element){
  26.         this.element = element;
  27.     }
  28.  
  29.     //method to update or setup link
  30.     void updateLink(node link){
  31.         this.link = link;
  32.     }
  33.  
  34.     //method to get the element from the node
  35.     int getElement(){
  36.         return this.element;
  37.     }
  38.  
  39.     //method to get the next node
  40.     node getNextNode(){
  41.         return this.link;
  42.     }
  43. }
  44.  
  45. class LL {
  46.  
  47.     Scanner sc = new Scanner(System.in);
  48.     node head;
  49.  
  50.     void insertAtStart(int number){
  51.  
  52.         node newNode = new node(number);
  53.  
  54.         if(head == null){
  55.             head = newNode;
  56.         } else {
  57.             newNode.updateLink(head);
  58.         }
  59.         head = newNode;
  60.  
  61.         return;
  62.     }
  63.  
  64.     void insertAtEnd(int element){
  65.         if(head == null){
  66.             this.insertAtStart(element);
  67.             return;
  68.         }
  69.         node current = this.head;
  70.         while(current.getNextNode() != null){
  71.             current = current.getNextNode();
  72.         }
  73.  
  74.         node newNode = new node(element);
  75.         current.updateLink(newNode);
  76.         return;
  77.     }
  78.  
  79.     void display(){
  80.         node current = this.head;
  81.  
  82.         while(current!= null){
  83.             System.out.print(current.getElement());
  84.             if(current.getNextNode() != null){
  85.                 System.out.print("-->");
  86.             }
  87.             current = current.getNextNode();
  88.         }
  89.     }
  90.  
  91.     // function to reverse k starting nodes from the linked list
  92.     void reverse_K_nodes(int k){
  93.         node current = this.head;
  94.         node next = null;
  95.         node prev = null;
  96.  
  97.         while(current != null && k != 0){
  98.             next = current.getNextNode();
  99.             current.updateLink(prev);
  100.             prev = current;
  101.             current = next;
  102.             k--;
  103.         }
  104.         this.head = prev;
  105.         current = this.head;
  106.         while(current.getNextNode() != null){
  107.             current = current.getNextNode();
  108.         }
  109.         current.updateLink(next);
  110.     }
  111. }
  112. public class LinkedList {
  113.     public static void main(String arg[]) {
  114.         System.out.println("Program to reverse first k nodes of Linked List.");
  115.  
  116.         LL l1 = new LL();
  117.         l1.insertAtEnd(1);
  118.         l1.insertAtEnd(2);
  119.         l1.insertAtEnd(3);
  120.         l1.insertAtEnd(4);
  121.         l1.insertAtEnd(5);
  122.         l1.insertAtEnd(6);
  123.         l1.insertAtEnd(7);
  124.         l1.insertAtEnd(8);
  125.         l1.display();
  126.  
  127.         System.out.println("\nThe linked list after reversing the first 3 nodes is : ");
  128.         l1.reverse_K_nodes(3);
  129.         l1.display();
  130.     }
  131. }
Add Comment
Please, Sign In to add comment