ProgrammingModerate
Building a list from n values by inserting each new node at the head is O(1) per insert. What is the order of the values when read from the head after inserting 1, 2, 3, 4 in that order?
- A. 1, 2, 3, 4
- B. 2, 1, 4, 3
- C. 4, 1, 2, 3
- D. 4, 3, 2, 1
Correct answer: D. 4, 3, 2, 1
Each new node goes in front of the previous head, so the last value inserted is read first.
More C Programming questions
- Which C statement is correct for a node with an int field v and a next pointer, allocated and initialised to v = 7 with no next node?
- A doubly linked list stores prev pointers. How many pointer fields must change to insert a new node between two adjacent nodes A and B?
- A singly linked list of N = 10^6 nodes must answer 10^6 queries for the value at position k. Which approach is correct and fast enough?
- A function frees an entire list and then sets head = NULL. Why is setting head to NULL useful?
- Which kind of list makes it O(1) to delete a node given only a pointer to that node?
- In a singly linked list, which operation takes O(1) time when a pointer to the head is given?