You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Node* reverse(Node* head) {
Node* p, *cur, *nn;
p = NULL;
cur = head;
nn = NULL;
while (cur != NULL) {
nn = cur->next;
cur->next = p;
p = cur;
cur = nn;
}
return p;
}
Node* addOne(Node *head) {
head = reverse(head);
Node* cur = head;
int carry = 1, sum = 0;
Node* t=head;
while (cur != NULL) {
sum = cur->data + carry;
cur->data = sum % 10;
carry = sum / 10;
if (carry == 0)
break;
t=cur;
cur = cur->next;
}
// After the while loopif (carry != 0) {
// If cur is null, it means we need to add a new node at the end of the listif (cur == NULL)
{
t->next= newNode(carry);
}
}
returnreverse(head);
}
METHOD 2 (Not working for large input)
longlong mod = 10e9 +7;
public:
Node* addOne(Node *head)
{
Node* cur = head;
if(head == NULL)
returnnewNode(1);
longlongint num = 0;
while(cur != NULL)
{
num = num * 10 + cur->data;
cur = cur->next;
}
Node* head1 = NULL;
//num = num % mod;
num++;
// num = num % mod;while( num != 0)
{
int rem = num%10;
num = num /10;
if( head1 == NULL)
head1 = newNode(rem);
else
{
Node* nn= newNode(rem);
nn->next = head1;
head1 = nn;
}
}
return head1;
}