From abb9b5d531ef7d0db3e0f2b028fba4ac45a417f1 Mon Sep 17 00:00:00 2001 From: Holychung Date: Fri, 20 May 2022 15:43:44 +0800 Subject: [PATCH] Update delete-the-middle-node-of-a-linked-list.cpp --- C++/delete-the-middle-node-of-a-linked-list.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/C++/delete-the-middle-node-of-a-linked-list.cpp b/C++/delete-the-middle-node-of-a-linked-list.cpp index e65c24b2de..09513d7407 100644 --- a/C++/delete-the-middle-node-of-a-linked-list.cpp +++ b/C++/delete-the-middle-node-of-a-linked-list.cpp @@ -15,3 +15,16 @@ class Solution { return dummy.next; } }; + +// Time: O(n) +// Space: O(1) +class Solution { +public: + ListNode *deleteMiddle(ListNode *head) { + ListNode **node = &head, *fast = head; + for (ListNode *fast = head; fast && fast->next; fast = fast->next->next) + node = &(*node)->next; + *node = (*node)->next; + return head; + } +}; \ No newline at end of file