Skip to content

Instantly share code, notes, and snippets.

@Kishy-nivas
Created April 22, 2018 16:12
Show Gist options
  • Save Kishy-nivas/b8ef4df0bbdd06adc497d88ad409d165 to your computer and use it in GitHub Desktop.
Save Kishy-nivas/b8ef4df0bbdd06adc497d88ad409d165 to your computer and use it in GitHub Desktop.
class GfG {
private Node reverse(Node head) {
Node curr = head;
Node prev = null;
Node next = null;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
public void addOne(Node head) {
if (head == null)
return;
Node curr = head;
int carry = 1;
Node prev = null;
while (curr != null) {
int sum = curr.data + carry;
if (sum > 9) {
carry = 1;
} else {
carry = 0;
}
curr.data = sum % 10;
prev = curr;
curr = curr.next;
}
if (carry > 0) {
prev.next = new Node(1);
prev.next.next = null;
//head = prev.next;
}
Node correct_head = reverse(head);
while (correct_head != null) {
System.out.print(correct_head.data);
correct_head = correct_head.next;
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment