336. Circular Linked List — Insert at Position

EasyLinked ListCircular Linked ListInsertionPointer Manipulation

Given a circular singly linked list (tail.next = head), an integer val, and a 1-based position pos, insert a new node with value val at position pos. Maintain the circular structure. Return the new head.

Position 1 = insert at beginning (before current head).

Input: Circular linked list (as array), integer val, integer pos (1-based).

Output: Updated circular linked list values from new head.

Examples

Example 1
Input: [1,2,4,5], 3, 3
Output: [1,2,3,4,5]
Explanation: Insert 3 at position 3 (between 2 and 4). Circular structure maintained.
Example 2
Input: [1,2,3], 0, 1
Output: [0,1,2,3]
Explanation: Insert 0 at position 1 (new head). Tail still points to new head.
Example 3
Input: [1,2,3], 4, 4
Output: [1,2,3,4]
Explanation: Insert 4 at end (position 4 = n+1). New tail.next = head.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →