Created
April 22, 2018 16:12
-
-
Save Kishy-nivas/b8ef4df0bbdd06adc497d88ad409d165 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
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