leetcode 876. Middle of the Linked List [Medium]
Contents
題目敘述
獲得 linked list 的 middle 節點。
經典的快慢指標問題,但是這題找到中間節點之後不需要前面的資料,所以直接用 head 來當 slow 指標。
解題流程
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode *fast = head;
while (fast && fast->next) {
fast = fast->next->next;
head = head->next;
}
return head;
}
};