You just need to return the head of the new linked list.
In case the linked list ends after adding K nodes, where K is any positive integer less than 'N'.Append the sum of those K nodes at the end of the linked list.
The first line of input contains an integer 'T' representing the number of Test cases.
The next 2 * 'T' lines denote the T test cases. Each test case consists of two lines.
The first line of each test case contains the elements of the Linked list separated by space and the linked list is terminated by -1.
The second line of each test case contains two integers separated by a single space denoting 'N' and 'M' respectively, where in the linked list, after every 'M' nodes, take the sum of the next 'N' nodes and append that sum to the linked list.
For each test case, return the modified linked list with elements separated by space.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= n <= 10^4
1 <= N <= 10^4
1 <= M <= 10^4
-10^5 <= VAL <= 10^5
Where 'n' denotes the length of the linked list and 'VAL' represents a node value of the linked list.
Time limit: 1 sec
Initially maintain two variables, βCOUNTMβ and βCOUNTNβ and Initialize them to β0β, where βCOUNTMβ will keep track of iterating βMβ nodes which are to be skipped and βCOUNTNβ will keep track of iterating βNβ nodes whose sum needs to be calculated. Also, maintain a boolean variable βTURNNβ representing which variable to count on, βNβ or βMβ. If the βTURNNβ variable is βTRUEβ we would increment on βCOUNTNβ else βCOUNTMβ. Set βTURNNβ to false because initially, we would increment βCOUNTMβ as we need to skip the first βMβ nodes of the linked list.
Create a helper function with the help of which we will be recursively traversing through the linked list and pass all the required variables to the helper function as displayed in the code. After βMβ recursive calls, βCOUNTMβ would be equal to βMβ, thereby which we will set βCOUNTMβ to β0β, βTURNNβ to βTRUEβ, and start incrementing βCOUNTNβ. If βCOUNTNβ reaches βNβ, append the sum of those βNβ nodes to the linked list. The base condition of the recursive helper function would be when we encounter the nodeβs next is βNULLβ. Here if βTURNNβ is βFALSEβ, we would simply return βHEADβ, otherwise, we would append the temporary sum to the end of the linked list and then return the βHEADβ.
Initially maintain two variables, βCOUNTMβ and βCOUNTNβ and Initialize them to β0β, where βCOUNTMβ will keep track of iterating βMβ nodes which are to be skipped and βCOUNTNβ will keep track of iterating βNβ nodes whose sum needs to be calculated. Also, maintain a boolean variable βTURNNβ representing which variable to count on, βNβ or βMβ. If βTURNNβ variable is βTRUEβ we would increment on βCOUNTNβ else βCOUNTMβ. Set βTURNNβ to βFALSEβ because initially, we would increment βCOUNTMβ as we need to skip first βMβ nodes of the linked list.
As soon as βCOUNTMβ is equal to βMβ, set βTURNNβ to βTRUEβ and set βCOUNTMβ again to β0β. Now keep a temporary variable to hold the sum of next βNβ nodes, and keep increasing βCOUNTNβ, and as soon as βCOUNTNβ reaches βNβ, append the sum contained in a temporary variable to the linked list, set βCOUNTNβ to β0β and βTURNNβ again to βFALSEβ. Keep repeating this process until we reach the end of the linked list. If we reach the end of the linked list before βCOUNTMβ reaches βMβ, there will be no changes in our answer, but if we reach the end before βCOUNTNβ reaches βN', then append the sum contained in the temporary variable to end of the linked list.
Deletion In Doubly Linked List
Deletion In Doubly Linked List
Insertion In Doubly Linked List
LRU Cache
Delete Nodes On Regular Intervals
Add One To Linked List