Description
Problem
Implement a program to handle layer-by-layer commit overwriting in Git.
You are given a singly linked list of commits, each with an id and a pointer to the next commit (head is the most recent).
Operations
- Rollback(k): Remove the latest
kcommits, returning the list starting from the new head. - Package(k): Merge the latest
kcommits into a single commit whose id is the concatenation of thosekcommit ids (from newest to oldest, joined with_), followed by the remaining list.
Examples
Initial list:
commit5 -> commit4 -> commit3 -> commit2 -> commit1
Rollback(2)
Output: commit3 -> commit2 -> commit1
Package(3)
Output: commit5_commit4_commit3 -> commit2 -> commit1
Constraints
- List length ≥ 1.
- Rollback count ≤ list length.
- Package count ≤ list length.
- All operations are valid inputs.
- Aim for O(k) for each operation and O(1) extra space.
Output
Return the modified linked list after the operation.
Notes
- Phone screen level.
- Keep implementation efficient and memory-conscious.
Loading editor…
OUTPUTLast run results appear here.
No output yet. Click "Run Code" to see results.