-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path876-middle-of-the-linked-list.cpp
37 lines (33 loc) · 1.15 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
// Title: Middle of the Linked List
// Description:
// Given a non-empty, singly linked list with head node head, return a middle node of linked list.
// If there are two middle nodes, return the second middle node.
// Link: https://leetcode.com/problems/middle-of-the-linked-list/
// Time complexity: O(n)
// Space complexity: 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) {
// (index of the middle node + 1) = (index of the last node + 1) / 2
// dummy node to shift the starting point with +1 index
ListNode dummyHead(0xDEADBEEF, head);
// it just looks cool :D
ListNode *slowNode = &dummyHead, *fastNode = &dummyHead;
while (
(slowNode = slowNode->next)
&& (fastNode = fastNode->next)
&& (fastNode = fastNode->next)
);
return slowNode;
}
};