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

删除链表中重复的结点

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

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pHead ListNode类 
 * @return ListNode类
 */
struct ListNode* deleteDuplication(struct ListNode* pHead) {
    struct ListNode* newhead = (struct ListNode*)malloc(sizeof(struct ListNode));
    if (pHead == NULL)return NULL;
    struct ListNode* newtail = newhead;
    newhead->next =NULL;
    struct ListNode* cur = pHead;
    if (pHead->next == NULL)return pHead;
    struct ListNode* next = cur->next;
    while (cur)
    {
        if (!next&&!cur)return newhead->next;
        if (next&&cur->val == next->val)
        {
                while (next&&cur->val==next->val)
                {
                    if (next)
                        next = next->next;
                }
                cur = next;
                if (next)
                    next = next->next;
        }
        else
        {
            newtail->next = cur;
            newtail = cur;
            newtail->next = NULL;
            cur = next;
            if (next)
                next = next->next;
        }
    }
    return newhead->next;
}

全部评论

相关推荐

2025-12-16 22:45
已编辑
电子科技大学 活动运营
Rain_Codin...:简历感觉有点乱了而且一股AI味,AI简历的一个特点就是废话很多,一个点能分成四个点来讲,可以仔细优化一下。 btw,手机看简历不好看出来,可以把电脑上的简历截图放出来。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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