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

删除链表中重复的结点

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;
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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