Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range Sum of Linked List #1874

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions docs/linked-list/range-sum-of-linked-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
id: range-sum-of-linked-list
title: Range Sum of Linked List
sidebar_label: Range Sum of Linked List
description: 'Calculate the sum of node values within this range.'
tags: [dsa, data-structures, LinkedList]
---

### Range Sum of Linked List

Given a linked list and a range defined by start and end (0-indexed), calculate the sum of the node values within this range.

### Implementation in c++

```cpp
#include <iostream>
using namespace std;

struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};

int rangeSum(ListNode* head, int start, int end) {
int rangeSum = 0; // Sum of values within the range
int position = 0; // Track the current position in the list
ListNode* current = head; // Pointer to traverse the list

// Traverse the linked list
while (current != nullptr) {
// If the position is within the specified range, add to sum
if (position >= start && position <= end) {
rangeSum += current->val;
}

// Break if we've passed the end of the range
if (position > end) {
break;
}

// Move to the next node and increment the position
current = current->next;
position++;
}

return rangeSum;
}

int main() {
// Creating a sample linked list: 1 -> 2 -> 3 -> 4 -> 5
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(3);
head->next->next->next = new ListNode(4);
head->next->next->next->next = new ListNode(5);

// Calculate the sum of node values from position 1 to 3 (inclusive)
int start = 1;
int end = 3;
cout << "Range Sum: " << rangeSum(head, start, end) << endl; // Output should be 2 + 3 + 4 = 9

// Clean up the memory allocated for the linked list
ListNode* temp;
while (head != nullptr) {
temp = head;
head = head->next;
delete temp;
}

return 0;
}
```

### Complexity

- **Time Complexity**: $O(n)$
- **Space Complexity**: $O(1)$
Loading