Skip to content

Commit cf6f786

Browse files
authored
Create Code Testcase Test Result Test Result 876. Middle of the Linked List
1 parent 24984c3 commit cf6f786

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* middleNode(ListNode* head) {
14+
ListNode *slow=head;
15+
ListNode *fast=head;
16+
17+
while(fast){
18+
fast=fast->next;
19+
if(fast){
20+
fast=fast->next;
21+
slow=slow->next;
22+
}
23+
}
24+
return slow;
25+
}
26+
};

0 commit comments

Comments
 (0)