题解 | #删除有序链表中重复的元素-II#

删除有序链表中重复的元素-II

http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    ListNode* deleteDuplicates(ListNode* head) {
        // write code here
        ListNode* node = new ListNode(-1);
        ListNode* pre = node;
        
        node->next = head;
        while (head && head->next)
        {
            if (head->val != head->next->val)
            {
                pre = head;
                head = head->next;
            }
            else
            {
                int n = head->val;
                while(head && head->val == n){
                    head = head->next;
                }
            }
            pre->next = head;
        }
        pre->next = head;
        return node->next;
    }
};
全部评论

相关推荐

04-11 23:51
门头沟学院 Java
坚定的芭乐反对画饼_许愿Offer版:人人都能过要面试干嘛,发个美团问卷填一下,明天来上班不就好了
点赞 评论 收藏
分享
好兄弟们,不愁找不到工作了,东哥还有10万骑手HC待发 还有五险一金,话不多说我要去投递了
婉拒腾讯保洁岗:都让让,鄙人骑电动车贼溜,ssp骑手offer应该有我一份吧?在坐的谁赞同,谁反对?查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务