-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiddle of Linked List.cpp
85 lines (66 loc) · 2.48 KB
/
Middle of Linked List.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Problem statement Link - https://leetcode.com/problems/middle-of-the-linked-list
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
/* Input : [1,2,3,4,5]
output : [3,4,5]
Input : [1,2,3,4,5,6]
output : [4,5,6]
if two middles, return 2nd middle node
*/
// Two Pointer
// Time : O(n); Space : O(1)
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode *slow = head; // Pointer slow is initialised with head of the linkedlist
ListNode *fast = head; // Pointer head is initialised with head of the linkedlist
while(fast != NULL && fast->next != NULL) // Loop till fast is Null or node next to fast is NULL
{
slow = slow->next; // slow is incremented by one node
fast = fast->next->next; // fast is incremented by two node, so slow will be in middle when fast reaches end of linkedlist
}
return slow; // return the node where slow points
}
};
/* Visualization
Given : 1 -> 2 -> 3 -> 4 -> 5
1 -> 2 -> 3 -> 4 -> 5 1 -> 2 -> 3 -> 4 -> 5 1 -> 2 -> 3 -> 4 -> 5 3 -> 4 -> 5
| => | | => | | => 3 is the middle node
slow slow fast slow fast of the Linkedlist
fast
Given : 1 -> 2 -> 3 -> 4 -> 5-> 6
1 -> 2 -> 3 -> 4 -> 5 -> 6 1 -> 2 -> 3 -> 4 -> 5 1 -> 2 -> 3 -> 4 -> 5 -> 6 1 -> 2 -> 3 -> 4 -> 5 -> 6 4 -> 5-> 6
| => | | => | | => | | => 4 is the second middle node
slow slow fast slow fast slow fast of the Linkedlist
(Here fast-> next is not NULL)
*/
*/
// Two Pass
// Time : O(n); Space : O(1)
class Solution {
public:
ListNode* middleNode(ListNode* head) {
int l=0;
ListNode* temp = head;
while (temp != NULL) {
temp = temp->next;
l++;
}
temp = head;
int count = 0;
// Incrementing temp length/2 times
while (count < l/2) {
temp = temp->next;
count++;
}
return temp;
}
};