FastPrepProcess Singly Linked List Operations

Process Singly Linked List Operations

SEDEMAC logoSEDEMAC● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Implement a singly linked list and process two equal-length arrays of operations. For operation type 1, insert values[i] at the front. For type 2, insert it at the back. For type 3, remove the front node when the list is nonempty; its paired value is ignored. Return the list values from front to back after all operations.

Maintain both head and tail pointers so each operation runs in constant time.

Function

processSinglyLinkedListOperations(operationTypes: int[], values: int[]) → int[]

Examples

Example 1

operationTypes = [2,2,1,3]values = [4,7,2,0]return = [4,7]

Append four and seven, prepend two, then remove the front node.

Example 2

operationTypes = [3,2,3,3]values = [0,8,0,0]return = []

Removing from an empty list is a no-op; the later inserted node is removed once.

Constraints

  • 1 <= operationTypes.length == values.length <= 200000
  • operationTypes[i] is 1, 2, or 3
  • values[i] is a signed 32-bit integer
  • values[i] is ignored when operationTypes[i] is 3

More SEDEMAC problems

See SEDEMAC hiring insights
public int[] processSinglyLinkedListOperations(int[] operationTypes, int[] values) {
    // Write your code here.
}
operationTypes[2,2,1,3]
values[4,7,2,0]
expected[4,7]
Checking account…