23. Sum of Last N Nodes of Linked List
The problem can be found at the following link: Question Link
Problem Description
Given a singly linked list, calculate the sum of the last n
nodes. It is guaranteed that n <= number of nodes
.
You are provided with a linked list and an integer
n
.The task is to calculate the sum of the last
n
nodes in the list.
Example 1:
Input: Linked List: 5->9->6->3->4->10
, n = 3
Output: 17
Explanation: The sum of the last three nodes in the linked list is 3 + 4 + 10 = 17
.
Example 2:
Input: Linked List: 1->2
, n = 2
Output: 3
Explanation: The sum of the last two nodes in the linked list is 2 + 1 = 3
.
My Approach
Two-Pointer Technique:
We use two pointers:
fast
andslow
. Thefast
pointer is movedn
nodes ahead from the start.After positioning the
fast
pointer, we move bothfast
andslow
pointers simultaneously until thefast
pointer reaches the end of the list. At this point, theslow
pointer will be positioned at the beginning of the lastn
nodes.Finally, traverse the remaining
n
nodes from theslow
pointer and calculate their sum.
Edge Cases:
If
n <= 0
, return0
as there are no nodes to sum.If the list is empty (
head == NULL
), return0
.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(L), where
L
is the total number of nodes in the linked list. This is because we traverse the linked list only once.Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of extra space (two pointers).
Code (C++)
class Solution {
public:
int sumOfLastN_Nodes(struct Node* head, int n) {
if (n <= 0 || head == NULL) return 0;
Node* fast = head;
Node* slow = head;
for (int i = 0; i < n; i++) {
if (fast == NULL) return 0;
fast = fast->next;
}
int sum = 0;
while (fast != NULL) {
fast = fast->next;
slow = slow->next;
}
while (slow != NULL) {
sum += slow->data;
slow = slow->next;
}
return sum;
}
};
Code (Java)
class Solution {
public int sumOfLastN_Nodes(Node head, int n) {
if (n <= 0 || head == null) return 0;
Node fast = head;
Node slow = head;
for (int i = 0; i < n; i++) {
if (fast == null) return 0;
fast = fast.next;
}
int sum = 0;
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
while (slow != null) {
sum += slow.data;
slow = slow.next;
}
return sum;
}
}
Code (Python)
class Solution:
def sumOfLastN_Nodes(self, head, n):
if n <= 0 or head is None:
return 0
fast = head
slow = head
for _ in range(n):
if fast is None:
return 0
fast = fast.next
sum_last_n = 0
while fast is not None:
fast = fast.next
slow = slow.next
while slow is not None:
sum_last_n += slow.data
slow = slow.next
return sum_last_n
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit my LinkedIn:- Any Questions. Thank you for your input; together, we strive to create a space where learning is a collaborative endeavor.
β Star this repository if you find it helpful or intriguing! β
πVisitor Count
Last updated