m2skills

add one ll java

Jan 28th, 2018
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 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.     void addOne(){
  111.         this.reverse();
  112.         addingOneUtil();
  113.         this.reverse();
  114.     }
  115.  
  116.     // adding one to linked list
  117.     private void addingOneUtil(){
  118.         int carry = 1;
  119.         int sum = 0;
  120.         node start = this.head;
  121.         while(start != null && carry != 0){
  122.             sum = start.getElement() + carry;
  123.             start.updateData(sum % 10);
  124.             carry = sum / 10;
  125.             start = start.getNextNode();
  126.         }
  127.         if(carry != 0){
  128.             this.insertAtEnd(carry);
  129.         }
  130.     }
  131. }
  132. public class LinkedList {
  133.     public static void main(String arg[]) {
  134.         System.out.println("Program to add one to a number represented by Linked List.");
  135.  
  136.         LL l1 = new LL();
  137.         l1.insertAtEnd(9);
  138.         l1.insertAtEnd(9);
  139.         l1.insertAtEnd(9);
  140.         l1.insertAtEnd(9);
  141.         l1.insertAtEnd(9);
  142.         l1.insertAtEnd(9);
  143.         l1.insertAtEnd(9);
  144.         l1.insertAtEnd(9);
  145.         l1.display();
  146.  
  147.         System.out.println("\nNumber after adding one is : ");
  148.         l1.addOne();
  149.         l1.display();
  150.     }
  151. }
Add Comment
Please, Sign In to add comment