Arrays & Linked List Flashcards
What is the difference between an array and a linked list?
Arrays allow random access (O(1)) but have fixed sizes; linked lists allow dynamic memory allocation but require sequential access (O(n)). Doubly linked lists can allow for o(1) ops
What is the time complexity of accessing an element in an array?
O(1) (constant time).
What is the time complexity of accessing an element in a singly linked list?
O(n) (linear time).
What are the advantages of a doubly linked list over a singly linked list?
Doubly linked lists allow backward traversal and easier deletion but use extra memory. Can allow for o(1) operations
How do you create a dummy node?
Initialize with let dummy = new ListNode(); let current = dummy;
.
How do you make a deep copy of a linked list?
- Interleave new nodes with original nodes.
- Assign random pointers for copied nodes.
- Separate original and copied lists to restore structure.