03 · OOP for DSA & Design Rounds

#707 Design Linked List

Mediumsolve on LeetCode ↗

frequently reported atMicrosoftAmazon

the problem

In plain words

Write your own MyLinkedList with 0-indexed operations. get(index) returns the value, or -1 if the index is invalid. addAtHead(val) and addAtTail(val) insert at the ends. addAtIndex(index, val) inserts before position index: index equal to the length appends, and a larger index does nothing. deleteAtIndex(index) removes the node if the index is valid. Don't use the built-in LinkedList.

in addAtHead(1), addAtTail(3), addAtIndex(1, 2), get(1), deleteAtIndex(1), get(1)

out null, null, null, 2, null, 3

The list goes [1] → [1, 3] → [1, 2, 3]; get(1) is 2. After deleting index 1 the list is [1, 3], so get(1) is 3.

  • 0 ≤ index, val ≤ 1000
  • At most 2000 calls in total
  • Do not use the built-in LinkedList

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #206
      Reverse Linked List
      Easyin Linked Lists
    2. #19
    3. #146
      LRU Cache
      Mediumin Linked Lists
    4. #622
      Design Circular Queue
      Mediumin Queues