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
nnodes 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:
fastandslow. Thefastpointer is movednnodes ahead from the start.After positioning the
fastpointer, we move bothfastandslowpointers simultaneously until thefastpointer reaches the end of the list. At this point, theslowpointer will be positioned at the beginning of the lastnnodes.Finally, traverse the remaining
nnodes from theslowpointer and calculate their sum.
Edge Cases:
If
n <= 0, return0as 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
Lis 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_nContribution 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