/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* deleteMiddle(ListNode* head) {
int num = 0;
ListNode *h = head;
while(h != NULL) {
num ++;
h = h->next;
}
if (num == 1) return NULL;
h = head;
int idx = num / 2;
for (int i = 0; i < idx - 1; i ++){
h = h->next;
}
ListNode *t = h->next;
h->next = t->next;
return head;
}
};
leetcode第270场周赛,5942 删除链表的中间节点
最新推荐文章于 2024-05-31 17:17:25 发布