Insert a Value at a Linked List Position
Updated
Problem statement
Given the head of a singly linked list, a value val and a position pos, insert the value at that position and return the head of the resulting list. Insert a new element; do not replace an existing value. Preserve the order and values of every existing element.
For this exercise, assume positions are zero-based. In a list of length n, a valid position ranges from 0 through n: position 0 inserts before the current head, and position n appends after the tail. An empty list accepts only position 0. The returned list has exactly n + 1 elements.
The list is represented as an array of values in examples. You may change next pointers or build an equivalent list; node identity is not part of the output contract.
Function
insertAtPosition(head: ListNode, val: int, pos: int) → ListNodeExamples
Example 1
head = [2,4,6]val = 9pos = 1return = [2,9,4,6]Insert 9 before the element at index 1, retaining every original value.
Example 2
head = [5,8]val = 3pos = 0return = [3,5,8]The inserted value becomes the new head.
Example 3
head = [1,2]val = 7pos = 2return = [1,2,7]Position 2 equals the list length, so append 7.
Example 4
head = []val = 4pos = 0return = [4]Inserting into the empty list produces one element.
Constraints
- For this exercise, assume an acyclic singly linked list containing
0 <= n <= 2000nodes. -10^9 <= Node.val, val <= 10^9.0 <= pos <= n; the position is always valid.
More Meta Onsite Interview questions
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
- Maximum Stock Profit with Transaction LimitsONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM interview questions
- Delete a Doubly Linked List Node at a PositionQualys · ONSITE INTERVIEWQualys interview questions
- Insert After a Linked-List NodeAMD · ONSITE INTERVIEWAMD interview questions
- Reverse a Singly Linked ListGoogle · ONSITE INTERVIEWGoogle interview questions
- Process Singly Linked List OperationsSEDEMAC · ONSITE INTERVIEW
- Merge Two Sorted Linked ListsZS · ONSITE INTERVIEWZS interview questions
- Middle Node Of A Linked ListNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Build and Check a Palindrome Linked ListOmnissa · ONSITE INTERVIEWOmnissa interview questions
- Reverse a Singly Linked ListGEP · ONSITE INTERVIEW
- Merge Two Sorted Linked ListsApple · ONSITE INTERVIEWApple interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences