题解 | #删除链表中重复的结点#

删除链表中重复的结点

https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef


/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead) {
        // 处理空链表
        if(!pHead){
            return nullptr;
        }
        ListNode *res = new ListNode(0);  // 建立虚表头
        res->next = pHead; // 链接
        ListNode *cur = res; // 使用cur来遍历链表
        while(cur->next && cur->next->next){
            if(cur->next->val == cur->next->next->val){
                int val = cur->next->val;
                while(cur->next && cur->next->val == val){
                    cur->next = cur->next->next;
                }
            }// cur->next为该情况下第一个不为重复值的节点
            else{
                cur = cur->next;
            }
        }
        return res->next;
    }
};

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 12:05
点赞 评论 收藏
分享
找到实习了 给了150一天 但是说是低代码 值得去吗
码农索隆:是在没实习,可去,待个一两周,不行就润呗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务