m2skills

reverse Linked List java

Jan 28th, 2018
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 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.     // function to reverse linked list
  93.     void reverse(){
  94.         if(this.head == null){
  95.             return;
  96.         }
  97.  
  98.         node nextNode = null;
  99.         node prev = null;
  100.         node current = this.head;
  101.         while(current != null){
  102.             nextNode = current.getNextNode();
  103.             current.updateLink(prev);
  104.             prev = current;
  105.             current = nextNode;
  106.         }
  107.         this.head = prev;
  108.     }
  109. }
  110. public class LinkedList {
  111.     public static void main(String arg[]) {
  112.         System.out.println("Program to reverse Linked List.");
  113.  
  114.         LL l1 = new LL();
  115.         l1.insertAtEnd(1);
  116.         l1.insertAtEnd(2);
  117.         l1.insertAtEnd(3);
  118.         l1.insertAtEnd(4);
  119.         l1.insertAtEnd(5);
  120.         l1.insertAtEnd(6);
  121.         l1.insertAtEnd(7);
  122.         l1.insertAtEnd(8);
  123.         l1.display();
  124.  
  125.         System.out.println("\nThe linked list after reversing is : ");
  126.         l1.reverse();
  127.         l1.display();
  128.     }
  129. }
Add Comment
Please, Sign In to add comment