Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iamutkarshtiwari/f1cb95b7fb2dd3a09917ee305e478cc9 to your computer and use it in GitHub Desktop.
Save iamutkarshtiwari/f1cb95b7fb2dd3a09917ee305e478cc9 to your computer and use it in GitHub Desktop.
/*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