Given a singly linked list and an integer **K**, the task is to remove all the continuous set of nodes whose sum is **K** from the given linked list. Print the updated linked list after the removal. If no such deletion can occur, print the original Linked list.

**Examples:**

Input:Linked List: 1 -> 2 -> -3 -> 3 -> 1, K = 3

Output:-3 -> 1

Explanataion:

The nodes with continuous sum 3 are:

1) 1 -> 2

2) 3

Therefore, after removing these chain of nodes Linked List becomes: -3-> 1

Input:Linked List: 1 -> 1 -> -3 -> -3 -> -2, K = 5

Output:1 -> 1 -> -3 -> -3 -> -2

Explanataion:

No continuous nodes exits with sum K

**Approach:**

- Append Node with value zero at the starting of the linked list.
- Traverse the given linked list.
- During traversal store the sum of the node value till that node with the reference of the current node in an unordered_map.
- If there is Node with value
**(sum – K)**present in the**unordered_map**then delete all the nodes from the node corresponding to value**(sum – K)**stored in map to the current node and update the sum as**0**. - If there is no Node with value
**(sum – K)**present in the unordered_map, then stored the current sum with node in the map.

Below is the implementation of the above approach:

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A Linked List Node ` `struct` `ListNode { ` ` ` `int` `val; ` ` ` `ListNode* next; ` ` ` ` ` `// Contructor ` ` ` `ListNode(` `int` `x) ` ` ` `: val(x), next(NULL) ` ` ` `{ ` ` ` `} ` `}; ` ` ` `// Function to create Node ` `ListNode* getNode(` `int` `data) ` `{ ` ` ` `ListNode* temp; ` ` ` `temp = (ListNode*)` `malloc` `(` `sizeof` `(ListNode)); ` ` ` `temp->val = data; ` ` ` `temp->next = NULL; ` ` ` `return` `temp; ` `} ` ` ` `// Function to print the Linked List ` `void` `printList(ListNode* head) ` `{ ` ` ` `while` `(head->next) { ` ` ` `cout << head->val << ` `" -> "` `; ` ` ` `head = head->next; ` ` ` `} ` ` ` `printf` `(` `"%d"` `, head->val); ` `} ` ` ` `// Function that removes continuos nodes ` `// whose sum is K ` `ListNode* removeZeroSum(ListNode* head, ` ` ` `int` `K) ` `{ ` ` ` `// Root node initialise to 0 ` ` ` `ListNode* root = ` `new` `ListNode(0); ` ` ` ` ` `// Append at the front of the given ` ` ` `// Linked List ` ` ` `root->next = head; ` ` ` ` ` `// Map to store the sum and reference ` ` ` `// of the Node ` ` ` `unordered_map<` `int` `, ListNode*> umap; ` ` ` ` ` `umap[0] = root; ` ` ` ` ` `// To store the sum while traversing ` ` ` `int` `sum = 0; ` ` ` ` ` `// Traversing the Linked List ` ` ` `while` `(head != NULL) { ` ` ` ` ` `// Find sum ` ` ` `sum += head->val; ` ` ` ` ` `// If found value with (sum - K) ` ` ` `if` `(umap.find(sum - K) != umap.end()) { ` ` ` ` ` `ListNode* prev = umap[sum - K]; ` ` ` `ListNode* start = prev; ` ` ` ` ` `// Delete all the node ` ` ` `// traverse till current node ` ` ` `int` `aux = sum; ` ` ` ` ` `// Traverse till current head ` ` ` `while` `(prev != head) { ` ` ` `prev = prev->next; ` ` ` `aux += prev->val; ` ` ` `if` `(prev != head) { ` ` ` `umap.erase(aux); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Update the start value to ` ` ` `// the next value of current head ` ` ` `start->next = head->next; ` ` ` ` ` `// Update sum to zero ` ` ` `sum = 0; ` ` ` `} ` ` ` ` ` `// If (sum - K) value not found ` ` ` `else` `{ ` ` ` `umap[sum] = head; ` ` ` `} ` ` ` ` ` `head = head->next; ` ` ` `} ` ` ` ` ` `// Return the value of updated ` ` ` `// head node ` ` ` `return` `root->next; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// head Node ` ` ` `ListNode* head; ` ` ` ` ` `// Create Linked List ` ` ` `head = getNode(1); ` ` ` `head->next = getNode(2); ` ` ` `head->next->next = getNode(-3); ` ` ` `head->next->next->next = getNode(3); ` ` ` `head->next->next->next->next = getNode(1); ` ` ` ` ` `// Given sum K ` ` ` `int` `K = 5; ` ` ` ` ` `// Function call to get head node ` ` ` `// of the updated Linked List ` ` ` `head = removeZeroSum(head, K); ` ` ` ` ` `// Print the updated Linked List ` ` ` `printList(head); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 -> 2 -> -3 -> 3 -> 1

* Time Complexity: O(N)*, where N is the number of Node in the Linked List.

*, where N is the number of Node in the Linked List.*

**Auxiliary Space Complexity:**O(N)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Delete N nodes after M nodes of a linked list
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Delete alternate nodes of a Linked List
- Delete all Prime Nodes from a Singly Linked List
- Delete all the even nodes from a Doubly Linked List
- Delete linked list nodes which have a greater value on left side
- Delete all Prime Nodes from a Doubly Linked List
- Delete all nodes from the doubly linked list which are divisible by K
- Delete linked list nodes which have a Lesser Value on Left Side
- Delete all the even nodes of a Circular Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Delete all odd or even positioned nodes from Circular Linked List
- Delete all Non-Prime Nodes from a Singly Linked List
- Delete all odd nodes of a Circular Linked List
- Program to delete all even nodes from a Singly Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Delete a given node in Linked List under given constraints
- Delete all the nodes from the list which are divisible by any given number K
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List

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.