Sudo Placement[1.4] | K Sum
Given head of a linked list of integers and an integer k, your task is to modify the linked list as follows:
- Consider nodes in groups of size k. In every group, replace value of first node with group sum.
- Also, delete the elements of group except the first node.
- During traversal, if the remaining nodes in linked list are less than k then also do the above considering remaining nodes.
Examples:
Input: N = 6, K = 2
1->2->3->4->5->6
Output: 3 7 11We have denoted grouping of k elements by (). The elements inside () are summed.
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
(1 -> 2) -> 3 -> 4 -> 5 -> 6 -> null
(3) -> 3 -> 4 -> 5 -> 6 -> null
3 -> (3 -> 4) -> 5 -> 6 -> null
3 -> (7) -> 5 -> 6 -> null
3 -> 7 -> (5 -> 6) -> null
3 -> 7 -> (11) -> null
3 -> 7 -> 11 -> null
Approach: Insert the given nodes in the Linked list. The approach of insertion has been discussed in this post. Once the nodes are inserted, iterate from the beginning of the list. Mark the first node as temp node. Iterate for the next k-1 nodes and sum up the nodes in sum variable. If the number of nodes is less than K, then replace the temp node’s data with sum and point temp to NULL. If there is a group with K nodes, replace the temp’s data with sum and move temp to the node which is just after K nodes. Continue the above operation till temp points to NULL. Once temp reaches the end, it means all groups of size K has been traversed and the node has been replaced with the sum of nodes of size K. Once the operation of replacements have been done, the linked list thus formed can be printed. The method of printing the linked list has been discussed in this post.
Below is the implementation of the above approach:
C++
// C++ program for // SP - K Sum #include <iostream> using namespace std; // structure for linked list struct Node { long long data; Node* next; }; // allocated new node Node* newNode(int key) { Node* temp = new Node; temp->data = key; temp->next = NULL; return temp; } // function to insert node in a Linked List Node* insertNode(Node* head, int key) { if (head == NULL) head = newNode(key); else head->next = insertNode(head->next, key); return head; } // traverse the node // to print it void print(Node* head) { // travere the linked list while (head) { cout << head->data << " "; head = head->next; } cout << endl; } // function to perform the following operation void KSum(Node* head, int k) { // initially pointing to start Node* temp = head; // iterate till end while (temp) { // dummy variable to store k long long count = k; // summation of nodes long long sum = 0; // currently at node from // which itearation is done Node* curr = temp; // till k nodes are visited and // last node is not visited while (count-- && curr) { // add to sum the node data sum = sum + curr->data; // move to the next node curr = curr->next; } // change pointer's data of temp to sum temp->data = sum; // move temp to next pointer temp->next = curr; // move temp to the next pointer temp = temp->next; } } // Driver Code int main() { Node* head = NULL; // inserts nodes in the linked list head = insertNode(head, 1); head = insertNode(head, 2); head = insertNode(head, 3); head = insertNode(head, 4); head = insertNode(head, 5); head = insertNode(head, 6); int k = 2; // Function call to perform the // given operations KSum(head, k); // print the linked list print(head); return 0; } |
Java
// Java program for // SP - K Sum import java.io.*; import java.util.*; // structure for linked list class Node { int data; Node next; Node(int key) { data = key; next = null; } } class GFG { // allocated new node static Node head; // function to insert node // in a Linked List public static Node insertNode(Node head, int key) { if (head == null) head = new Node(key); else head.next = insertNode(head.next, key); return head; } // traverse the node // to print it public static void print(Node head) { // travere the linked list while (head != null) { System.out.print(head.data + " "); head = head.next; } System.out.println(); } // function to perform // the following operation public static void KSum(Node head, int k) { // initially pointing // to start Node temp = head; // iterate till end while (temp != null) { // dummy variable // to store k int count = k; // summation of nodes int sum = 0; // currently at node from // which itearation is done Node curr = temp; // till k nodes are visited and // last node is not visited while (count > 0 && curr != null) { // add to sum the node data sum = sum + curr.data; // move to the next node curr = curr.next; count--; } // change pointer's data // of temp to sum temp.data = sum; // move temp to // next pointer temp.next = curr; // move temp to // the next pointer temp = temp.next; } //return temp; } // Driver Code public static void main(String args[]) { head = null; // inserts nodes in // the linked list head = insertNode(head, 1); head = insertNode(head, 2); head = insertNode(head, 3); head = insertNode(head, 4); head = insertNode(head, 5); head = insertNode(head, 6); int k = 2; // Function call to perform // the given operations KSum(head, k); // print the linked list print(head); } } |
C#
// C# program for SP - K Sum using System; // structure for linked list public class Node { public int data; public Node next; public Node(int key) { data = key; next = null; } } public class GFG { // allocated new node static Node head; // function to insert node // in a Linked List public static Node insertNode(Node head, int key) { if (head == null) head = new Node(key); else head.next = insertNode(head.next, key); return head; } // traverse the node // to print it public static void print(Node head) { // travere the linked list while (head != null) { Console.Write(head.data + " "); head = head.next; } Console.WriteLine(); } // function to perform // the following operation public static void KSum(Node head, int k) { // initially pointing // to start Node temp = head; // iterate till end while (temp != null) { // dummy variable // to store k int count = k; // summation of nodes int sum = 0; // currently at node from // which itearation is done Node curr = temp; // till k nodes are visited and // last node is not visited while (count > 0 && curr != null) { // add to sum the node data sum = sum + curr.data; // move to the next node curr = curr.next; count--; } // change pointer's data // of temp to sum temp.data = sum; // move temp to // next pointer temp.next = curr; // move temp to // the next pointer temp = temp.next; } // return temp; } // Driver Code public static void Main() { head = null; // inserts nodes in // the linked list head = insertNode(head, 1); head = insertNode(head, 2); head = insertNode(head, 3); head = insertNode(head, 4); head = insertNode(head, 5); head = insertNode(head, 6); int k = 2; // Function call to perform // the given operations KSum(head, k); // print the linked list print(head); } } /* This code contributed by PrinciRaj1992 */ |
Output:
3 7 11
Recommended Posts:
- Sudo Placement[1.5] | Partition
- Sudo Placement[1.5] | Wolfish
- Sudo Placement[1.4] | BST Traversal
- Sudo Placement[1.3] | Playing with Stacks
- Sudo Placement[1.3] | Final Destination
- Sudo Placement[1.3] | Stack Design
- Sudo Placement | Beautiful Pairs
- Sudo Placement 2 | Matrix Series
- Sudo Placement[1.5] | Second Smallest in Range
- Sudo Placement[1.4] | Jumping the Subtree
- Sudo Placement | Range Queries
- Sudo Placement | Palindrome Family
- Sudo Placement | Special Subsequences
- Sudo Placement[1.7] | Greatest Digital Root
- Sudo Placement | Placement Tour
This article is contributed by Team Geeksforgeeks. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : princiraj1992



