Sum consecutive duplicates in a linked list
viaLeetCode
Problem Given a linked list, collapse each run of consecutive equal values into a single node holding their sum — in place, without auxiliary structures (no frequency map).
Input / Output
- Input: head of singly linked list.
- Output: head of the collapsed list.
Constraints
- O(n) time, O(1) extra space — the no-map restriction is explicit.
Example
- 1→2→2→3→3→3→9 → 1→4→9.
Expected approach
- Single pass: for each node, while next has the same ORIGINAL value, add it into the current node and splice it out. Subtlety: compare against the run's original value, not the growing sum (2→2→4 must not merge the 4 into the summed 4) — track runValue separately. O(n)/O(1). Edge cases: all-equal list, single node, runs at the tail.
asked …