Interleave a Toutiao Feed List by Alternating From Both Ends
Last updated: November 29, 2026
Quick Overview
Given a singly linked list of feed items L0 -> L1 -> ... -> Ln, reorder it in place to L0 -> Ln -> L1 -> Ln-1 -> ... so fresh and older items alternate, using O(1) extra space.
ByteDance
November 29, 20260
0
0 solved
Given a singly linked list of feed items L0 -> L1 -> ... -> Ln, reorder it in place to L0 -> Ln -> L1 -> Ln-1 -> ... so fresh and older items alternate, using O(1) extra space.
Toutiao's feed sometimes blends items from both ends of a ranked list to mix freshness with depth. The real test is classic linked-list surgery: finding the middle, reversing a half, and merging, all in place, which ByteDance interviewers use to check pointer discipline under time pressure.
What the Interviewer Expects
- Find the middle with slow and fast pointers
- Cut the list and reverse the second half in place
- Merge the two halves alternately
- Avoid creating cycles by terminating the first half
- Keep O(1) extra space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you do it with O(n) extra space, and when is that acceptable?
- Generalize to taking k items from the front, then k from the back.
- How would you test this function to catch cycle bugs quickly?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: head of a singly linked list, possibly empty or a single node. Output: the same head with nodes relinked, no new nodes created, O(1) extra space. Values are opaque....
High-Level Design
Three steps. First, slow and fast pointers stop slow at the end of the first half (for odd length, the first half is one longer). Second, set slow.next = None and reverse the remainder. Third, weave...