Add K Nodes Problem Statement
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. The value of the new node should be the sum of the previous 'K' nodes.
Note:
If the number of nodes remaining in the linked list is less than 'K' but greater than zero, insert a node with the value equal to the sum of the remaining nodes.
Input:
Element list: a sequence of integers followed by -1, signifying the end (e.g., 1 2 3 -1)
K: an integer indicating the number of nodes to consider
Output:
Modified linked list: elements space-separated and terminated by -1 (e.g., 1 2 3 6 -1)
Example:
Input:
1 2 3 4 5 -1
K = 2
Output:
1 2 3 4 7 5 5 -1
Constraints:
0 ≤ L <= 5*105
where 'L' is the number of nodes in the linked list.-103 ≤ data ≤ 103
and data is not equal to -1.1 ≤ K ≤ 106
- Time Limit: 1 sec

AnswerBot
4mo
Modify a singly linked list by inserting a new node after every 'K' nodes with the sum of previous 'K' nodes.
Traverse the linked list while keeping track of 'K' nodes at a time
Calculate the sum of the...read more
Help your peers!
Add answer anonymously...
Micron Technology Associate Software Engineer interview questions & answers
An Associate Software Engineer was asked Q. Explain OOP concepts with example code.
An Associate Software Engineer was asked Q. Garbage Collection in Java SQL queries
An Associate Software Engineer was asked Q. Add K Nodes Problem Statement You are given a singly linked list of integers and...read more
Popular interview questions of Associate Software Engineer
An Associate Software Engineer was asked Q1. Explain OOP concepts with example code.
An Associate Software Engineer was asked Q2. Garbage Collection in Java SQL queries
An Associate Software Engineer was asked Q3. Add K Nodes Problem Statement You are given a singly linked list of integers and...read more
>
Micron Technology Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

