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

  • 20Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Linked List Basics

What is the fundamental building block of a linked list?

20 questions

  1. 1

    What is the fundamental building block of a linked list?

    1. A

      An array of elements stored contiguously in memory

    2. B

      A collection of key-value pairs

    3. C

      A node containing a value and a reference to the next node

    4. D

      A pointer to the head of the list

    Correct answer
    C

    A node containing a value and a reference to the next node

    Explanation

    A linked list is composed of nodes, each storing a value and a pointer (next) to the subsequent node. This structure allows nodes to be scattered in memory.

  2. 2

    In a singly linked list, what does the 'next' pointer of the tail node point to?

    1. A

      Null, indicating the end of the list

    2. B

      A special sentinel node

    3. C

      The previous node, forming a doubly linked list

    4. D

      The head node, forming a circular list

    Correct answer
    A

    Null, indicating the end of the list

    Explanation

    The tail node's next pointer is set to null to mark the end of the linked list. This is a fundamental property of singly linked lists.

  3. 3

    Which property of a linked list is essential for traversing it from start to end?

    1. A

      Each node has a reference to the next node

    2. B

      Nodes are stored contiguously in memory

    3. C

      The list maintains a pointer to the last node

    4. D

      Each node stores the index of its position

    Correct answer
    A

    Each node has a reference to the next node

    Explanation

    Traversal relies on each node's 'next' pointer to move sequentially. Starting from the head and following next pointers until null visits every node.

  4. 4

    When inserting a new node into the middle of a singly linked list, how many pointer reassignments are typically needed?

    1. A

      All nodes after the insertion point must be updated

    2. B

      Only one: the new node's next pointer

    3. C

      Three: the previous, new, and next node's pointers

    4. D

      Two: the previous node's next and the new node's next

    Correct answer
    D

    Two: the previous node's next and the new node's next

    Explanation

    Insertion requires setting the new node's next to point to the node after the insertion point, and then updating the previous node's next to point to the new node. This is O(1) work regardless of list size.

  5. 5

    Why is inserting an element at the beginning of an array considered O(n) in the worst case?

    1. A

      Because arrays cannot insert at the beginning by design

    2. B

      Because the new element must be placed at the end of the array

    3. C

      Because all existing elements must be shifted one position to the right

    4. D

      Because the array must be resized to accommodate the new element

    Correct answer
    C

    Because all existing elements must be shifted one position to the right

    Explanation

    Arrays store elements contiguously; inserting at index 0 requires shifting every existing element one index to the right, taking time proportional to the number of elements (n).

  6. 6

    What is a key advantage of a linked list over an array for insertions and deletions?

    1. A

      Linked lists automatically resize when full

    2. B

      Linked lists can grow without needing contiguous memory

    3. C

      Linked lists allow constant-time access to any element by index

    4. D

      Linked lists use less memory than arrays

    Correct answer
    B

    Linked lists can grow without needing contiguous memory

    Explanation

    Linked lists do not require contiguous memory; each node can be allocated independently. This makes insertions and deletions efficient (O(1) if you have a reference) without shifting other elements.

  7. 7

    In the iterative traversal of a linked list, what is the correct while loop condition to process all nodes?

    1. A

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

    2. B

      while (current !== head)

    3. C

      while (current.next !== null)

    4. D

      while (current !== null)

    Correct answer
    D

    while (current !== null)

    Explanation

    Using 'current !== null' ensures that when current becomes null (after the last node), the loop stops. Checking 'current.next' would skip the last node because the condition would fail when current is the tail.

  8. 8

    In the recursive traversal of a linked list, what is the base case that stops the recursion?

    1. A

      When the node's value matches a target

    2. B

      When the head argument is null

    3. C

      When the next pointer is null

    4. D

      When the list has been traversed for n steps

    Correct answer
    B

    When the head argument is null

    Explanation

    The base case for recursive traversal is when head is null, which represents an empty list or the end of the list. This prevents infinite recursion and triggers the return.

  9. 9

    In the linked list values problem, which approach creates only a single array to collect results without copying?

    1. A

      Iterative approach using a while loop and pushing values

    2. B

      Both iterative and recursive approaches inherently copy arrays

    3. C

      Recursive approach that concatenates arrays

    4. D

      Recursive approach that returns a new array at each step

    Correct answer
    A

    Iterative approach using a while loop and pushing values

    Explanation

    The iterative solution uses a single array and pushes each node's value into it as it traverses. A recursive solution that creates a new array per call would be inefficient; the better recursive version passes a mutable array by reference.

  10. 10

    What is the time and space complexity of the iterative sum list algorithm (assuming n nodes)?

    1. A

      Time O(n), Space O(n)

    2. B

      Time O(1), Space O(n)

    3. C

      Time O(n^2), Space O(1)

    4. D

      Time O(n), Space O(1)

    Correct answer
    D

    Time O(n), Space O(1)

    Explanation

    The iterative sum list visits each node once (O(n) time) and uses only a few variables (sum, current), so space is O(1). The recursive version would use O(n) space due to the call stack.

  11. 11

    In the linked list find problem, what is the correct pattern for returning true or false?

    1. A

      Return false if the first node doesn't match, otherwise continue the loop

    2. B

      Use a counter and return true only if the target appears more than once

    3. C

      Return true if the target is found, otherwise return false after the loop

    4. D

      Return true at the end of the loop if target was never found

    Correct answer
    C

    Return true if the target is found, otherwise return false after the loop

    Explanation

    The standard pattern is to check each node; if the target matches, return true immediately. If the loop ends without finding it, return false. Returning false inside the loop after a non-match would cause incorrect early exits.

  12. 12

    In the recursive solution for the linked list find problem, what are the two base cases?

    1. A

      Head is null (return false) and head.val equals target (return true)

    2. B

      Head.next is null (return false) and head.val equals target (return true)

    3. C

      Head is null (return null) and head.val equals target (return the node)

    4. D

      Head is null (return true) and head.val equals target (return false)

    Correct answer
    A

    Head is null (return false) and head.val equals target (return true)

    Explanation

    The recursive base cases are: if head is null, the target is not found (return false). If head.val equals target, return true. Otherwise, recurse on head.next.

  13. 13

    In the get node value problem, how do you handle an index that is out of bounds?

    1. A

      Return null

    2. B

      Throw an exception

    3. C

      Return the value of the last node

    4. D

      Return undefined

    Correct answer
    A

    Return null

    Explanation

    When the index exceeds the length of the linked list, the function should return null to indicate an invalid index. This is implemented by returning null when head becomes null (in recursive) or after the loop (in iterative).

  14. 14

    What is the key difference between the iterative and recursive approaches for the get node value problem regarding the index variable?

    1. A

      Both count up from 0

    2. B

      Both count down from the target index

    3. C

      Iterative counts down, recursive counts up

    4. D

      Iterative counts up from 0, recursive counts down from the target index

    Correct answer
    D

    Iterative counts up from 0, recursive counts down from the target index

    Explanation

    The iterative approach initializes a counter at 0 and increments it as it moves through nodes. The recursive approach decrements the index with each recursive call until it reaches 0, at which point it returns the current node's value.

  15. 15

    In the reverse list problem, what does the 'prev' variable initially point to?

    1. A

      A temporary dummy node

    2. B

      The tail of the list

    3. C

      The head of the list

    4. D

      Null

    Correct answer
    D

    Null

    Explanation

    The 'prev' variable is initialized to null because the original head will become the tail, and its next pointer should point to null after reversal.

  16. 16

    Why is it necessary to save a reference to the next node before overwriting current.next in the reverse list algorithm?

    1. A

      To avoid losing access to the rest of the list

    2. B

      To create a backup of the original list

    3. C

      To use as the new 'prev' in the next iteration

    4. D

      To verify the length of the list

    Correct answer
    A

    To avoid losing access to the rest of the list

    Explanation

    When we set current.next = prev, we lose the link to the original next node. By saving it in a temporary variable (e.g., 'next'), we can still progress through the list by setting current = next.

  17. 17

    At the end of the iterative reverse list algorithm, what variable holds the new head of the reversed list?

    1. A

      next

    2. B

      head

    3. C

      current

    4. D

      prev

    Correct answer
    D

    prev

    Explanation

    After the loop ends (current becomes null), the 'prev' variable points to the last node visited, which was the original tail. That node becomes the new head of the reversed list.

  18. 18

    In the zipper list problem, which list's first node is always the head of the result?

    1. A

      The shorter list

    2. B

      List one (head1)

    3. C

      The longer list

    4. D

      List two (head2)

    Correct answer
    B

    List one (head1)

    Explanation

    The problem specifies that zipping always starts with the first node of list one. The resulting list begins with head1 and then alternates.

  19. 19

    In the iterative zipper list solution, what condition exits the main while loop?

    1. A

      All nodes from both lists have been processed

    2. B

      Either current pointer is null

    3. C

      The count reaches the length of the longer list

    4. D

      Both current pointers are not null

    Correct answer
    B

    Either current pointer is null

    Explanation

    The while loop continues as long as both current pointers are not null. When one list runs out, the loop exits, and we then attach the remaining nodes from the other list.

  20. 20

    What is the time complexity of the zipper list algorithm in terms of the lengths of the two lists (n and m)?

    1. A

      O(n + m)

    2. B

      O(max(n, m))

    3. C

      O(min(n, m))

    4. D

      O(n * m)

    Correct answer
    C

    O(min(n, m))

    Explanation

    The algorithm alternates until the shorter list is exhausted, so it runs for O(min(n, m)) steps. The remaining nodes are attached in O(1) time, not requiring additional traversal.

Question 1 of 20
Question 1 of 20

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent