-
Notifications
You must be signed in to change notification settings - Fork 0
/
876-Middle-of-the-Linked-List.cpp
43 lines (37 loc) · 1.09 KB
/
876-Middle-of-the-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
// Author : Vicen-te
// Date : 09/10/2022
/*
Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
Ex.
Input: head = [1,2,3,4,5]
Output: [3,4,5]
1.- Set two pointers (one takes the next node / one by one,
two takes the next node from the next node / two by two)
2.- Check the position of two or the one next to two
3.- Continue until the two position or the one next to two is null
Time: O(N)
Space: O(1)
*/
/**
* 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) {}
* };
*/
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode* one = head;
ListNode* two = head;
while (two != NULL && two->next != NULL) {
one = one->next;
two = two->next->next;
}
return one;
}
};