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

删除链表中重复的结点

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

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

            }
            if (next) {
                next = cur->next;
            }
        }
    }
    return pHead;
}

全部评论

相关推荐

浅白lw:其实是牛马自己换马了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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