Skip to content

Commit fd972c7

Browse files
Time: 0 ms (100.00%), Space: 9.3 MB (12.77%) - LeetHub
1 parent 0346475 commit fd972c7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
while (fast != NULL && fast->next != NULL) {
17+
slow = slow->next;
18+
fast = fast->next->next;
19+
}
20+
return slow;
21+
}
22+
};

0 commit comments

Comments
 (0)