Add K Nodes
You are given a Singly Linked List of integers and an integer 'K'.
Your task is to modify the linked list by inserting a new node after every 'K' node in the linked list with the node value being equal to the sum of the previous 'K' nodes.
Note :
If you reach a situation when the number of nodes remaining in the linked list is less than 'K' but greater than zero, just insert a node with the value equal to the sum of the remaining nodes.
Input Format
The first line of the input contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
The second line contains a single integer 'K'.
Output Format:
Print the modified linked list.
The elements of the modified list should be single-space separated, terminated by -1.
Note:
You are not required to print the output, it has already been taken care of. Just implement the function.
Constraints:
0 <= L <= 5 * 10^5
-10^3 <= data <= 10^3 and data != -1
1 <= K <= 10^6
where 'L' is the number of nodes in the linked list and 'data' is the value of elements present in the given linked list.
Time Limit: 1 sec
CodingNinjas
author
2y
Naive approach
- We will iterate through the linked list and store the sum of the nodes in a variable 'CURR_SUM' (initialized as zero) and keep a check of the number of nodes traversed since the last ins...read more
Help your peers!
Add answer anonymously...
Top Micron Technology Associate Software Engineer interview questions & answers
Popular interview questions of Associate Software Engineer
>
Micron Technology Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app