aboutsummaryrefslogtreecommitdiff
path: root/delete_middle_node.cpp
blob: 1493046779e36dcdf7633d816d98f46e1773b57f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "leetcode.h"

class Solution {
public:
  ListNode *deleteMiddle(ListNode *head) {
    if (head->next == nullptr)
      return nullptr;
    auto slow = head, fast = head->next->next;
    while (fast != nullptr && fast->next != nullptr) {
      fast = fast->next->next;
      slow = slow->next;
    }
    slow->next = slow->next->next;
    return head;
  }
};