- What are the methods (or ways) by which you find the middle element of the list? (For example, if the element has 11 elements, 6th element is the middle. If you have 10 elements, 5th or 6th (whichever you like) is the middle element of the list).
- What is algorithmic complexity of each method?
take 2 pointers initially pointing to the head, increment the pointer p1 twice with proper null checks and increment the p2 once, when the pointer p1 points to the last element of the list, p2 points to the middle element. complexity is O(n)
ReplyDeleteLet me know if am wrong :)
@Laxmi
ReplyDeleteYep, you are correct