Created
January 22, 2017 02:49
-
-
Save iamutkarshtiwari/f1cb95b7fb2dd3a09917ee305e478cc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Complete the given Function | |
Node is as follows | |
class Node{ | |
int data; | |
Node next; | |
Node(int d){ | |
data=d; | |
next=null; | |
} | |
}*/ | |
class GfG{ | |
public void addOne(Node head){ | |
int carry = 0; | |
carry = add(head); | |
if (carry == 1) { | |
Node newHead = new Node(1); | |
newHead.next = head; | |
head = newHead; | |
} | |
} | |
public int add(Node head) { | |
int carry; | |
if (head == null) { | |
return 1; | |
} | |
head.data += add(head.next); | |
if (head.data == 10) { | |
head.data = 0; | |
carry = 1; | |
} else { | |
carry = 0; | |
} | |
return carry; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment