![]() |
VOOZH | about |
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
Explanation:
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
Explanation:
No continuous nodes exits with sum K
Approach:
Below is the implementation of the above approach:
1 -> 2 -> -3 -> 3 -> 1
Time Complexity: O(N), where N is the number of Node in the Linked List.
Auxiliary Space Complexity: O(N), where N is the number of Node in the Linked List.