解題思路
看到找 linked list 中間節點,就知道是使用 fast-slow pointer。
程式碼
class Solution {
public:
ListNode* deleteMiddle(ListNode* head) {
if(head->next == nullptr)
return nullptr;
ListNode *fast, *slow, *prev;
fast = slow = head;
while(fast && fast->next)
{
prev = slow;
slow = slow->next;
fast = fast->next->next;
}
prev->next = slow->next;
return head;
}
};
沒有留言:
張貼留言