Sum consecutive duplicates in a linked list

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.
asked …
LeaderboardSalaryAccount