ed list. Write code to increment the number by 1 in-place(i.e. without using extra space).   Note: You don't need to print the elements, just update the elements and return the head of updated LL.   Input Constraints:   1 <= Length of Linked List <=10^6.   Input format :   Line 1 : Linked list elements (separated by space and terminated by -1)   Output Format :   Line 1: Updated lin

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Given a large number represented in the form of a linked list. Write code to increment the number by 1 in-place(i.e. without using extra space).
  Note: You don't need to print the elements, just update the elements and return the head of updated LL.
  Input Constraints:
  1 <= Length of Linked List <=10^6.
  Input format :
  Line 1 : Linked list elements (separated by space and terminated by -1)
  Output Format :
  Line 1: Updated linked list elements
  Sample Input 1 :
  3 9 2 5 -1
  Sample Output 1 :
  3 9 2 6
  Sample Input 2 :
  9 9 9 -1
  Sample Output 1 :
  1 0 0 0
  Solutions:///////////
  /***************
  * Following is the Node class already written
  class LinkedListNode<T> {
  T data;
  LinkedListNode<T> next;
   
  public LinkedListNode(T data) {
  this.data = data;
  }
  }
  ***************/
   
  public class Solution {
   
   
  public static LinkedListNode<Integer> nextLargeNumber(LinkedListNode<Integer> head) {
  LinkedListNode<Integer> temp;
  LinkedListNode<Integer> prev =null;
  LinkedListNode<Integer> curr=head;
  while(curr!=null){
  temp=curr.next;
  curr.next=prev;
  prev=curr;
  curr=temp;
  }
  LinkedListNode<Integer> tem=prev;
  int extra=0;
  LinkedListNode<Integer> a= new LinkedListNode<Integer>(1);
  while(tem!=null){
  if(tem.data==9){
  tem.data=0;
  extra=1;
  if(tem.next==null){
  tem.next=a;
  }
  }else{
  if(extra==1){
  int d=tem.data+1;
  if(d==10){
  tem.data=0;
  extra=1;
  }else{
  extra=0;
  }
  }else{
  tem.data=tem.data+1;
  break;
  }}
 

tem=tem.next;

 

 

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Lists
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education