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

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

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

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

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    ListNode* deleteDuplicates(ListNode* head) {
        // write code here
       if(head == nullptr || head->next == nullptr)return head;
        auto dummy = new ListNode(0);
        dummy->next = head;
        ListNode* f = head;
        ListNode* s = head;
        while(f != nullptr){
            if(f->val == s->val){
                f = f->next;
            }
            else if(f->val != s->val){
                s->next = f;
                s = s->next;
            }
        }
        s->next = nullptr;
        return dummy->next;
    }
};

全部评论

相关推荐

一天代码十万三:这都不能算简历吧
点赞 评论 收藏
分享
03-16 22:00
武汉大学 C++
幸福的小熊猫想要offer:我阿里投的 c++岗,面试官说自己是做 java 的,c++这辈子才有了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务