面试必考精华版Leetcode2095. 删除链表的中间节点

题目:

面试必考精华版Leetcode2095. 删除链表的中间节点_第1张图片


 

代码(首刷看解析 day22):

class Solution {
public:
    ListNode* deleteMiddle(ListNode* head) {
    if(head->next==nullptr) return nullptr;
    ListNode *right=head;
    ListNode *left=head;
    ListNode *NodeBeforeLeft;
    while(right!=nullptr&&right->next!=nullptr){
        right=right->next->next;
        NodeBeforeLeft=left;
        left=left->next;
    }
    NodeBeforeLeft->next=NodeBeforeLeft->next->next;
    return head;
    }
};

知识点:链表节点都是指针,定义要加*

你可能感兴趣的:(#,leetcode,---medium,面试,链表,职场和发展)