题解 | #删除有序链表中重复的元素-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) return head;
        ListNode *tem = head;
        //遍历指针
        while(tem->next != nullptr){
            if(tem->val == tem->next->val){
                //若重复,则改变指针指向,跳过重复元素
                tem->next = tem->next->next;
            }else{
                tem = tem->next;
            }
        }
        return head;
    }
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务