Move the Nth Linked-List Node to the Front
Updated
Problem statement
Given the head of a nonempty singly linked list and a one-based position n, move the actual node at position n to the front and return the new head. The first node has position 1.
Change links rather than copying or swapping node values. Every other node must keep its relative order. When n = 1, return the original list unchanged. The supplied position is always valid.
Function
moveNthToFront(head: ListNode, n: int) → ListNodeExamples
Example 1
head = [1,2,3,4]n = 3return = [3,1,2,4]Moved or new
Relink the selected node before the old head; every other node keeps its relative order.
Example 2
head = [8]n = 1return = [8]Relink the selected node before the old head; every other node keeps its relative order.
Example 3
head = [5,5,7]n = 3return = [7,5,5]Moved or new
Relink the selected node before the old head; every other node keeps its relative order.
Constraints
1 <= number of nodes <= 100000.1 <= n <= number of nodes.-10^9 <= node.val <= 10^9.- The list is acyclic.
More LinkedIn Phone Screen questions
- Maximum Product of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
- Maximum Sum of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
- Merge Two Sorted ListsPHONE SCREEN · Seen Nov 2026
- Sqrt(x)PHONE SCREEN · Seen Nov 2026
- Total Years of Work ExperiencePHONE SCREEN · Seen Nov 2026
- Sum an Arbitrary-Dimensional ArrayPHONE SCREEN · Seen Oct 2026
- Known Words Matching Phone DigitsPHONE SCREEN · Seen Oct 2026
- Inverse-Depth Nested List SumPHONE SCREEN · Seen Oct 2026
- Valid Number with ExponentsPHONE SCREEN · Seen Oct 2026
- Sort a Quadratic-Transformed ArrayPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Reverse Linked ListByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Middle of a Linked ListCisco · PHONE SCREENCisco interview questions
- Reverse a Linked ListApple · PHONE SCREENApple interview questions
- Merge Two Sorted ListsIBM · PHONE SCREENIBM interview questions
- Reverse a Singly Linked ListChubb · PHONE SCREENChubb interview questions
- Remove All Repeated Values from a Linked ListArista Networks · PHONE SCREENArista Networks interview questions
- Detect a Cycle in a Linked ListSalesforce · PHONE SCREENSalesforce interview questions
- Find the Intersection Node of Two Linked ListsVisa · PHONE SCREENVisa interview questions
- Reverse a Linked ListMygate · PHONE SCREENMygate interview questions
- Remove Even-Positioned Linked-List NodesOracle · PHONE SCREENOracle interview questions