Data Structures & AlgorithmsEasyWipro · Accenture pattern
What is the time complexity of deleting the last node of a singly linked list of n nodes when only a head pointer is kept?
- A. O(1) – the tail is reachable directly
- B. O(log n)
- C. O(n²)
- D. O(n) – the second-last node must be found to update its next pointer
Correct answer: D. O(n) – the second-last node must be found to update its next pointer
Even if a tail pointer existed, removing the tail requires its predecessor, which a singly linked list can only reach by walking from the head. A doubly linked list with a tail pointer makes this O(1).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A stack is implemented with a singly linked list. Where should push and pop operate to keep both O(1)?
- What is printed? (C strings) c char s[] = "Placement"; printf("%zu %zu", strlen(s), sizeof(s));
- What is printed? (pointer arithmetic on an int array) c int a[] = {10, 20, 30, 40, 50}; int p = a + 1; printf("%d ", (p + 2)); printf("%d…
- What is printed? (2-D array in row-major order) python R, C = 3, 4 base, size = 1000, 4 def addr(i, j, rowmajor=True): return base + size…
- What is printed? (Python string immutability and methods) python s = "Hello World" t = s.replace("o", "0", 1) u = s.swapcase()[::2]…
- What is printed? (queue simulation – hot potato with step k) python from collections import deque q = deque(range(1, 8)); out = [] while…