删除单链表中的重复节点,并保留最开始出现的节点

思路:定义两个指针cur和p来逐个遍历链表,cur元素依次和p比较,直到p为NULL,cur向后移动一个。

struct listnode* delete_double_node(struct listnode* H)
{
    if(H==NULL)  return NULL; //判断是否需要遍历
    struct listnode* cur=H;
    while(cur)
    {
        struct listnode* p=cur;
        while(p->next)
        {
            if(p->next->data==cur->data)
            {
                p->next=p->next->next;
            }else
            {
                p=p->next;
            }
        cur=cur->next;
        }
    }

    return H;
}
全部评论

相关推荐

ldyllic:飞神,985+美团+腾讯+京东,无敌飞飞神
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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