Tell me how to find middle element of linked list in one pass?
Answer / Brijesh Kumar Nim
To find the middle element of a singly linked list in one pass, you can use two pointers. One pointer moves twice as fast as the other. Initially, both pointers start from the head node. After some time, the faster pointer will be at the end, and the slower pointer will be exactly at the middle. Here's a simple implementation in Python:
```python
def findMiddle(head):
slow = head
fast = head
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next
return slow
```
| Is This Answer Correct ? | 0 Yes | 0 No |
What is hash value of a string?
what is atmost complete binary tree?
What is numeric array?
What is the purpose of thread?
Why do we use data structures?
What is nsmutablearray?
For which header list, the last node contains the null pointer?
What is an array vs list?
Is quicksort greedy algorithm?
What is a bubble sort and how do you perform it?
When should structures be passed by values or by reference?
Can arraylist store objects?