-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleOfLL.cpp
64 lines (54 loc) · 1.36 KB
/
middleOfLL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//first approach
int getLength(Node* head){
int len = 0;
while(head != NULL){
len++;
head = head -> next;
}
return len;
}
Node* findMidlle(Node* head){
int len = getLength(head);
int ans = (len/2);
Node* temp = head;
int cnt = 0;
while(cnt < ans){
temp = temp -> next;
cnt++;
}
return temp;
}
/* 2nd approach (slow and fast pointer approach)
1) slow pointer vo hota hai jo same time me fast me aadha distance cover
karta hai.
2) fast pointer vo hota hai jo same time me slow se two times distance
cover karta hai.
*/
Node* getMiddle(Node* head){
if(head == NULL || head -> next == NULL){
return head;
}
//2 nodes - neede or not?
/* Ans:- ofcourse this step is needed because when there are
only two nodes, in that case jab loop run karega to length/2
tak jaayega aur length/2 ki value 1 hogi. To head pointer dusre
node par chala jayega aur wahi apna ans hoga uss case me.
so, this case can be handled simply before only.
*/
if(head -> next -> next == NULL){
return head -> next;
}
Node* slow = head;
Node* fast = head -> next;
while(fast != NULL){
fast = fast -> next;
if(fast != NULL){
fast = fast -> next;
}
slow = slow -> next;
}
return slow;
}
Node* findMiddle(Node* head){
return getMiddle(head);
}