leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2095.cpp (482B)


      1 class Solution {
      2     ListNode *pre_mid(ListNode *head) {
      3         ListNode *slow = head, *fast = head->next;
      4         while (fast && fast->next && fast->next->next) {
      5             slow = slow->next;
      6             fast = fast->next->next;
      7         }
      8         return slow;
      9     }
     10 
     11   public:
     12     ListNode *deleteMiddle(ListNode *head) {
     13         if (!head || !head->next) return nullptr;
     14         ListNode *pre = pre_mid(head);
     15         pre->next = pre->next->next;
     16         return head;
     17     }
     18 };