Validate Linked List Pointer Ordering
Problem statement
Arrays encode one singly linked list: next[i] is the next node index and head its first node. Every node is visited exactly once. Auxiliary backward[i] and forward[i] are node indices or -1 for null.
Return true if every non-null backward pointer targets an earlier list position and every non-null forward pointer targets a later position.
Function
validPointerOrder(next: int[], backward: int[], forward: int[], head: int) → booleanExamples
Example 1
next = [1,2,-1]backward = [-1,0,0]forward = [2,2,-1]head = 0return = trueAll auxiliary pointers point in their required list direction.
Constraints
- All arrays have equal length.
- All nonnegative indices are valid.