题解 | #删除有序链表中重复的元素-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;
    }
};

全部评论

相关推荐

02-11 14:29
已编辑
字节跳动_QA
Edgestr:这种的写代码最狠了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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