Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 2095. Delete the Middle Node of a Linked List
題目:

解答:
/**
* 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* deleteMiddle(ListNode* head) {
ListNode* d = new ListNode(0, head);
ListNode* slow = d;
ListNode* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
slow->next = slow->next->next;
return d->next;
}
};