703 views
1 1 vote

consider a linked list of the form

Where F is pointer to the first element in the list and L is a pointer to the last element in the list.

The time of which of the following operations depends on the length of the list?

A) Delete the last element of the list

B) Delete the first element of the list

C) Add an element after the last element of the list

D) Add an element before first element

1 Answer

0 0 votes

A)O(length) Delete the last element.

Becasue we have no other way in O(1) to move L pointer to the second last and delete the last element. What we can do,

  • Assign an extra pointer in the first position (say P)
  • Forward P towards the end of list and.
  • Stop at 2nd last. (how ? just check P->next->next = NULL ).
  • Delete Last node and assign P to L.
  • => O(n) => depends on the length of the linked list.
Position:
Show:

Related questions

1 1 vote
1 1 answer
11.6k
11.6k views
Pranabesh Ghosh 1 asked Aug 30, 2016
11,606 views
Suppose we are sorting an array of ten integers using a some quadratic sorting algorithm. After four iterations of the algorithm's main loop, the array elements are order...
0 0 votes
2 2 answers
1.3k
1.3k views
Pranabesh Ghosh 1 asked Aug 30, 2016
1,296 views
Given an array of integers what is the worst case time complexity that would find pair of integers which are same?A) O(nlogn)B) O(n)C) O()D) O(nloglogn)
2 2 votes
1 answers 1 answer
552
552 views
Pranabesh Ghosh 1 asked Aug 30, 2016
552 views
f(char a[10]) { int i = sizeof(a); printf("%d\n", i); }Assuming pointer size is 4bytes, what is the output of the code if function f is calledA) 4B) 40C) 1D) 10
4 4 votes
1 answers 1 answer
1.3k
1.3k views
Pranabesh Ghosh 1 asked Aug 30, 2016
1,318 views
A min heap with 1000 elements is stored in an array. What is the maximum possible index number for 9th min element?A) 254B) 100C) 9D) 511