1. Home
  2. Explore
  3. Computer Science
  4. Linked Lists Introduction

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Linked Lists Introduction

  • 41Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Linked List Fundamentals

What is a linked list?

41 questions

  1. 1

    What is a linked list?

    Model answer

    A linked list is an ordered data structure made up of nodes linked together via pointers.

    Explanation

    A linked list consists of nodes where each node contains data and a reference (next pointer) to the next node, forming a sequence.

  2. 2

    What is a node in a linked list?

    Model answer

    A node is a container for data, storing a value and a reference (next pointer) to the next node in the list.

    Explanation

    A node holds the actual data (e.g., a number, string, or object) and a link to the next node. The last node's next pointer is null.

  3. 3

    Define the head and tail of a linked list.

    Model answer

    The head is the first node of the linked list, and the tail is the last node, whose next pointer is null.

    Explanation

    The head provides the starting point for traversal. The tail marks the end of the list; its next pointer is null to indicate no further nodes.

  4. 4

    How is the end of a linked list represented?

    Model answer

    The last node's next pointer is set to null.

    Explanation

    Setting the last node's next to null signals that there are no more nodes after it, allowing algorithms to detect the end of the list.

  5. 5

    Is a linked list an ordered data structure? Explain.

    Model answer

    Yes, linked lists are ordered; each node has a position (starting at 0) determined by following the next pointers from the head.

    Explanation

    The order is maintained by the chain of next pointers. Starting at the head and following next pointers gives a fixed sequence of nodes.

  6. 6

    How does memory allocation differ between arrays and linked lists?

    Model answer

    Arrays require contiguous memory locations, while linked list nodes can be scattered anywhere in memory.

    Explanation

    Array elements are stored consecutively, which affects insertion/deletion costs. Linked list nodes are independent objects linked by pointers, so they don't need contiguous memory.

  7. 7

    What is the time complexity of inserting an element into an array at index 0? Why?

    Model answer

    O(n), because all existing elements must be shifted one position to the right to make room.

    Explanation

    Since arrays store elements contiguously, inserting at the beginning requires moving every element, leading to linear time in the worst case.

  8. 8

    What is the time complexity of inserting a node into a linked list at a given position? Why?

    Model answer

    O(1), assuming you have a reference to the node at the insertion point, because only two pointers need to be adjusted.

    Explanation

    Insertion only requires changing the next pointers of the previous node and the new node, a constant number of operations regardless of list length.

  9. 9

    When inserting into a linked list, why do you not need to shift subsequent nodes?

    Model answer

    Because nodes are not stored contiguously; you simply reroute pointers to include the new node.

    Explanation

    The new node's next pointer is set to the following node, and the previous node's next pointer is set to the new node, preserving the rest of the list without moving any data.

  10. 10

    What variable is essential for iteratively traversing a linked list?

    Model answer

    A 'current' pointer that starts at the head and is updated to current.next each iteration.

    Explanation

    The current pointer allows step-by-step access to each node. It is initialized to the head and moved forward via current = current.next.

  11. 11

    What is the correct loop condition for an iterative linked list traversal?

    Model answer

    while (current !== null) (or while (current != null)).

    Explanation

    The loop continues as long as current points to a node. When current becomes null, all nodes have been visited.

  12. 12

    What is the recursive base case for traversing a linked list?

    Model answer

    if (head === null) return;

    Explanation

    When head is null, the list is empty or we've reached the end, so the recursion stops.

  13. 13

    How do you process each node in a recursive traversal?

    Model answer

    Process the current node's value (e.g., print head.val), then recursively call the function on head.next.

    Explanation

    The recursion handles the current node first, then delegates the rest of the list to the recursive call on the next node.

  14. 14

    What is a common mistake in writing the loop condition for traversal?

    Model answer

    Using current.next !== null instead of current !== null, which skips the last node.

    Explanation

    Checking current.next causes the loop to stop before processing the last node because when current is the tail, current.next is null and the loop exits without processing the tail.

  15. 15

    How do you iteratively compute the sum of all values in a linked list?

    Model answer

    Initialize sum = 0, traverse with current starting at head, for each node add current.val to sum, then update current = current.next.

    Explanation

    The traversal accumulates values. The loop runs while current is not null, adding each node's value to the total sum.

  16. 16

    What is the base case for recursively summing a linked list?

    Model answer

    if (head === null) return 0;

    Explanation

    An empty list has a sum of zero, which serves as the termination condition.

  17. 17

    Write the recursive step to sum a linked list.

    Model answer

    return head.val + sumList(head.next);

    Explanation

    The sum of the list is the current node's value plus the sum of the remaining list (starting from the next node).

  18. 18

    What are the time and space complexities of the iterative sum list solution?

    Model answer

    Time: O(n), Space: O(1).

    Explanation

    We traverse each node once (O(n)) and use only a few variables (constant space).

  19. 19

    What are the time and space complexities of the recursive sum list solution?

    Model answer

    Time: O(n), Space: O(n) due to the call stack.

    Explanation

    Each recursive call adds a frame to the call stack, leading to linear space. Time is linear because each node is visited once.

  20. 20

    How do you iteratively check if a target value exists in a linked list?

    Model answer

    Traverse with current pointer; if current.val equals target, return true; after the loop, return false.

    Explanation

    If the target is found during traversal, we return true immediately. If we finish the loop without finding it, we return false.

  21. 21

    What are the two base cases for recursively finding a target in a linked list?

    Model answer
    1. if (head === null) return false; 2. if (head.val === target) return true;

    Explanation

    The first base case handles an empty list or end of list. The second base case returns true if the current node matches the target.

  22. 22

    Write the recursive step for the linked list find problem.

    Model answer

    return find(head.next, target);

    Explanation

    If the current node is not the target, we recursively search the remainder of the list starting from head.next.

  23. 23

    Why should you return false only after the loop in the iterative find solution?

    Model answer

    Because you must check every node before concluding the target is absent; returning false inside the loop would skip remaining nodes.

    Explanation

    If you return false when the first node doesn't match, you never check the rest. The false return must come after the entire list is traversed.

  24. 24

    What are the time and space complexities of the iterative find solution?

    Model answer

    Time: O(n), Space: O(1).

    Explanation

    In the worst case, we traverse all nodes (O(n)) and use only a constant amount of extra space.

  25. 25

    How do you iteratively get the value at a given index in a linked list?

    Model answer

    Initialize count = 0 and current = head. While current is not null, if count equals index return current.val; otherwise increment count and set current = current.next.

    Explanation

    We count nodes as we traverse. When the count matches the target index, we return that node's value. If we reach the end, the index is out of bounds.

  26. 26

    What should be returned if the index is out of bounds in the get node value problem?

    Model answer

    null.

    Explanation

    If the loop ends without finding the index, the index is invalid, so we return null (or undefined in some languages).

  27. 27

    Describe the recursive approach for get node value.

    Model answer

    If head is null, return null. If index is 0, return head.val. Otherwise, recursively call getNodeValue(head.next, index - 1).

    Explanation

    We decrement the index with each recursive call. When index reaches 0, we return the current node's value. If we hit null first, the index is out of range.

  28. 28

    What are the time and space complexities of the iterative get node value solution?

    Model answer

    Time: O(n), Space: O(1).

    Explanation

    In the worst case, we traverse the entire list (O(n)) and use constant space.

  29. 29

    What variables are needed to reverse a linked list iteratively?

    Model answer

    previous (initialized to null), current (initialized to head), and next (to store current.next temporarily).

    Explanation

    Previous tracks the reversed portion, current moves through the list, and next saves the next node before overwriting current.next.

  30. 30

    What are the steps to reverse a linked list iteratively?

    Model answer
    1. Save next = current.next. 2. Set current.next = previous. 3. Move previous = current. 4. Move current = next. Repeat until current is null.

    Explanation

    Each iteration reverses one node's pointer, then advances the pointers. After the loop, previous becomes the new head.

  31. 31

    After the iterative reversal loop, which variable points to the new head?

    Model answer

    previous.

    Explanation

    When the loop ends, current is null and previous points to the original tail, which becomes the new head of the reversed list.

  32. 32

    Write the recursive version of reverse list with a default parameter.

    Model answer

    function reverseList(head, previous = null) { if (head === null) return previous; const next = head.next; head.next = previous; return reverseList(next, head); }

    Explanation

    The base case returns previous (the new head). Otherwise, we save next, reverse the current node's pointer, and recurse with next as the new head and current node as the new previous.

  33. 33

    What are the complexities of the iterative reverse list solution?

    Model answer

    Time: O(n), Space: O(1).

    Explanation

    We traverse all n nodes once and use a fixed number of variables.

  34. 34

    What is the goal of the zipper linked lists problem?

    Model answer

    To merge two linked lists in place by alternating nodes, starting with the first node of list1.

    Explanation

    The resulting list interleaves nodes from both source lists. If one list is longer, its remaining nodes are appended at the end.

  35. 35

    What variables are needed for the iterative zipper lists solution?

    Model answer

    current1 (list1), current2 (list2), tail (tracks the end of the merged list), and a count variable (to decide which list to take from).

    Explanation

    current1 and current2 traverse each list. tail helps append nodes. The count's parity determines whether the next node comes from list2 (even) or list1 (odd).

  36. 36

    How does the count variable determine which list to take from in the zipper algorithm?

    Model answer

    If count is even, take from list2; if odd, take from list1.

    Explanation

    Starting with count=0 (even), we first take from list2 because we already started with list1's head. The pattern alternates as count increments.

  37. 37

    What should you do when one of the lists runs out during zipper merging?

    Model answer

    Append all remaining nodes of the other list to the end of the merged list.

    Explanation

    Once a list is exhausted, we simply attach the rest of the non-empty list to the tail to finish the merge.

  38. 38

    Write the base cases for the recursive zipper lists solution.

    Model answer

    if (head1 === null && head2 === null) return null; if (head1 === null) return head2; if (head2 === null) return head1;

    Explanation

    If both are null, return null. If one is null, return the other (the remainder of that list).

  39. 39

    Write the recursive step for zipper lists.

    Model answer

    const next1 = head1.next; const next2 = head2.next; head1.next = head2; head2.next = zipperLists(next1, next2); return head1;

    Explanation

    We link head1 to head2, then recursively merge the remaining tails. Finally, return head1 as the new head of the merged sublist.

  40. 40

    What is the time complexity of the iterative zipper lists solution?

    Model answer

    O(min(n, m)), where n and m are the lengths of the two lists.

    Explanation

    The loop runs until the shorter list is exhausted; handling the remainder is O(1). So the time is linear in the length of the shorter list.

  41. 41

    What is the space complexity of the recursive zipper lists solution?

    Model answer

    O(n + m) due to the call stack.

    Explanation

    Each recursive call adds a stack frame. In the worst case (when one list is empty early), the recursion depth can be up to the sum of both lists' lengths.

1 of 41
Question 1 of 41

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent