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!
Select
Add answer anonymously...

Top Associate Software Engineer Interview Questions Asked at Micron Technology

Q. Explain OOP concepts with example code.
Q. Garbage Collection in Java SQL queries
Q. Nth Fibonacci Number Problem Statement Calculate the Nth term in the Fibonacci s...read more
Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits