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