Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 29, 2018 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/3f6ef6b57885fffcf0c1 to your computer and use it in GitHub Desktop.
Save thmain/3f6ef6b57885fffcf0c1 to your computer and use it in GitHub Desktop.
public class AddLinkedListForwardOrder {
public int carry=0;
public Node newHead = null;
public Node add(Node h1, Node h2){
//first we will make sure that both the Linked list has same no of nodes
// to ensure that we will append 0 in front of shorter list
int h1Len = getLength(h1);
int h2Len = getLength(h2);
if(h1Len>h2Len){
int diff = h1Len-h2Len;
while(diff>0){
Node n = new Node(0);
n.next = h2;
h2=n;
diff--;
}
}
if(h1Len<h2Len){
int diff = h2Len-h1Len;
while(diff>0){
Node n = new Node(0);
n.next = h1;
h1=n;
diff--;
}
}
Node newHead = addBackRecursion(h1, h2);
//check for the carry forward, if not 0 then we need to create another node for the end
//example adding 1->1 and 9->9 then recursive function will return 0->0 and carry =1
if(carry!=0){
Node n = new Node(carry);
n.next = newHead;
newHead = n;
}
return newHead;
}
public Node addBackRecursion(Node h1, Node h2){
if(h1==null && h2==null){
return null;
}
addBackRecursion(h1.next, h2.next);
int a = h1.data + h2.data + carry;
carry=0;
//System.out.println(a);
if(a>=10){
carry =1;
a = a%10;
}
Node n = new Node(a);
if(newHead==null){
newHead =n;
}else{
n.next = newHead;
newHead = n;
}
//carry=0;
return newHead;
}
public int getLength(Node head){
int len=0;
while(head!=null){
len++;
head = head.next;
}
return len;
}
public void display(Node head){
Node currNode = head;
while(currNode!=null){
System.out.print("->" + currNode.data);
currNode=currNode.next;
}
}
public static void main(String args[]){
AddLinkedListForwardOrder l = new AddLinkedListForwardOrder();
Node h1 = new Node(1);
h1.next= new Node(1);
h1.next.next = new Node(1);
h1.next.next.next = new Node(7);
System.out.print("First Number : ");
l.display(h1);
Node h2 = new Node(9);
h2.next= new Node(9);
h2.next.next = new Node(9);
h2.next.next.next = new Node(9);
System.out.print("\n Second Number : ");
l.display(h2);
Node x = l.add(h1, h2);
System.out.print("\n Addition : ");
l.display(x);
}
}
class Node{
public int data;
public Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment