21 views
List: - [1, 9, 9, 9]
Result:- [2, 0, 0, 0]

Step1:- First of all convert the given linked list to number by using getNumFromLinkedList() method.
Step2:- And add number like 1 in it.
Step3:- Now, get the sum of actual number. Then convert number to linked list by using getNodeListFromSum() method. Finally, you get the result.

/**
* @param args
*/
public static void main(String[] args) {
list.insert(1);
list.insert(9);
list.insert(9);
list.insert(9);
System.out.println("Original List: - ");
list.print();

}

/**
* This program converts linked list to number, get sum of given number with list
* and again converts number to linked list.
*
* @param list
* @param num
* @return
*/
Node current = list;
int sum = listToNum + num;
Node node = getNodeListFromSum(sum);
return node;
}

private Node getNodeListFromSum(int sum) {
Node finalNode = null;
while(sum != 0){
int rem = sum % 10;
sum = sum / 10;
}
return finalNode;
}

private Node addFirst(Node node, int num){
Node newNode = new Node(num);
newNode.next = node;
node = newNode;
return node;
}

Node current = node;
int num = 0, count = 0;
while(current != null){
if(count == 0)
num = current.data;
else
num = num * 10 + current.data;
current = current.next;
count++;
}
return num;
}

private class Node{
int data;
Node next;
Node(int data){
this.data = data;
}
}

public void insert(int data){
Node newNode = new Node(data);
}else{
while(curr.next != null){
curr = curr.next;
}
curr.next = newNode;
}
}

public void print(){
return;
while(curr != null){
System.out.print(curr.data + " ");
curr = curr.next;
}
}
}
Output:-
Original List: -
1 9 9 9
2 0 0 0
answered Feb 19, 2018 by (3,520 points)
See the following running example to add a number in given linked list.

Step1:- Reverse the given linked list.
Step2:- Add given number like : 1.
Step3:- Again reverse the modified linked list and return modified list.

/**
* @param args
*/
public static void main(String[] args) {
list.insert(1);
list.insert(9);
list.insert(9);
list.insert(9);
System.out.println("Original List: - ");
list.print();

}

/**
* Reverse given linked list. 1-> 9-> 9 -> 9 to 9-> 9 -> 9 ->1
* Add given number like : 1
* Reverse modified linked list and return after modification
*
* @param list
* @param num
* @return
*/
Node current = list;
//Reverse given linked list. 1-> 9-> 9 -> 9 to 9-> 9 -> 9 ->1
Node prev = getReverseList(current);
//Add given number like : 1
//Reverse modified linked list and return after modification
}

Node current;
current = prev;
int carry = 0;
int count = 0;
int sum = 0;
while(current != null){
if(count == 0){
sum = current.data + num ;
}else{
sum = current.data + carry ;
}

if(sum > 9){
carry = 1;
sum = sum%10;
}else{
carry = 0;
}
current.data = sum;
current = current.next;
}
return prev;
}

private Node getReverseList(Node current) {
Node prev = null;
while(current != null){
Node tempNext = current.next;
current.next = prev;
prev = current;
current = tempNext;
}
return prev;
}

private class Node{
int data;
Node next;
Node(int data){
this.data = data;
}
}

public void insert(int data){
Node newNode = new Node(data);
}else{
while(curr.next != null){
curr = curr.next;
}
curr.next = newNode;
}
}

public void print(){
return;