ProgrammingModerate
A linked list is reversed with the recursive function rev(n): if (!n || !n->next) return n; h = rev(n->next); n->next->next = n; n->next = NULL; return h. For the list 1 -> 2 -> 3, what is the new head value?
- A. 1
- B. 3
- C. 2
- D. 0
Correct answer: B. 3
The recursion reaches the last node first, and that node becomes the new head. The base case returns the original tail.
More C Programming questions
- A malloc'd array of 10 structs is later passed to free(&arr[3]). What is the correct description?
- Which declaration allocates a 3 x 4 int matrix as one contiguous block with a single free needed?
- A list of N nodes is freed with free() on each node but the next pointer is read after freeing it. Which order is correct?
- C(0) = 1 and C(n) = sum over i from 0 to n-1 of C(i) C(n-1-i). What is C(4)?
- A 3 x 3 grid holds 1 to 9 row by row, so row 0 is 1 2 3, row 1 is 4 5 6 and row 2 is 7 8 9. What is the sum of the 2 x 2 block with rows 1…
- In the sorted array {1, 2, 4, 6, 8, 9}, how many unordered pairs of distinct elements sum to 17?