/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
// create LL
Node head = createNewNode(9);
Node two = createNewNode(0);
Node three = createNewNode(0);
Node nine = createNewNode(9);
head.next = two;
two.next = three;
three.next = nine;
Node reversedHead = reverseLL(head);
printLL(reversedHead);
incrementLL(reversedHead);
Node normal = reverseLL(reversedHead);
printLL(normal);
}
public static void incrementLL(Node head)
{
Node current = head;
Node previous = current;
int carry = 1;
while( current != null && carry > 0 )
{
int temp = current.value;
temp += carry;
carry = temp / 10;
temp %= 10;
current.value = temp;
previous = current;
current = current.next;
}
if( carry > 0 )
{
Node last = createNewNode(carry);
previous.next = last;
}
}
public static void printLL(Node head)
{
Node current = head;
while( current != null)
{
System.
out.
println("value is :" + current.
value); current = current.next;
}
System.
out.
println("-------"); }
public static Node reverseLL(Node head)
{
Node current = head;
Node previous = null;
while( current != null )
{
Node temp = current.next;
current.next = previous;
previous = current;
current = temp;
}
return previous;
}
public static Node createNewNode(int value)
{
Node node = new Node(value);
return node;
}
}
class Node
{
int value;
Node next;
public Node(int val)
{
value = val;
}
}