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

删除链表中重复的结点

http://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) {
        ListNode* vhead = new ListNode(-1);
        vhead->next = pHead;
        ListNode* pre = vhead;
        ListNode* cur = pHead;
        while(cur != nullptr){
            if(cur->next != nullptr && cur->val == cur->next->val){
                cur = cur->next;
                while(cur->next != nullptr && cur->val == cur->next->val){
                    cur = cur->next;
                }
                cur = cur->next;
                pre->next = cur;
                
            }else{
                pre = cur;
                cur = cur->next;
            }
        }
        return vhead->next;
    }
};
全部评论

相关推荐

10-31 20:07
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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