
Input: ‘N’ = 3, ‘head’ = [2, 3, 4]
Output: [2, 3, 5]
Explanation: Given number is 234. After adding 1 to it, it becomes 235.
First-line contains 'T', denoting the number of Test cases.
For each Test case:
The first line contains one integer, ‘N’, denoting the number of nodes in the Linked List.
The following line contains ‘N’ integers, denoting the non-negative integer (or the values of nodes of the linked list).
Return the head of the linked list after adding 1 to the non-negative integer.
You don't need to print anything. Just implement the given function.
1 <= T <= 10
1 <= N <= 100
0 <= Node.value <= 9
The number in the linked list doesn’t contain any leading zeroes except the number 0, where there’s only one node with the value 0.
Time Limit: 1-sec
We will use a dummy node whose initial value equals 0 and point to our linked list's head. We will use two more temporary nodes, specifically, ‘i’ and ‘j’, initially assigned to the dummy node.
First, we try to find the farthest node from the head of the linked list using node ‘j’, which is not equal to ‘9’. We assign it to ‘i’.
This is because, then, we can say that, after adding 1 to our given integer, the value of all the nodes after the node ‘i’ to the last node of the linked list becomes equal to ‘0’, but the value of the node ‘i’ increase by 1, and don’t become 0 as it’s initial value is not 9. This way, we don’t have to think about the nodes before node ‘i’.
After doing this, we can return the ‘dummy’ node or the node next to the ‘dummy’ node if the value of the dummy node is 0 (which means there is no need for an extra digit in the linked list).
// Function to add one to the integer represented by Linked List
function addOneToLinkedList(int n, ListNode* head):
Insertion In Doubly Linked List
Insertion In Doubly Linked List
Insertion In Doubly Linked List
Insertion In Doubly Linked List
Insertion In Doubly Linked List
LRU Cache
Delete Nodes On Regular Intervals
Implement Queue Using Linked List
Doubly to Singly Linked List Conversion