一、题目考察的知识点

链表遍历

二、题目解答方法的文字分析

题目的意思是要删除链表中重复值,那么只需要遍历即可,如果遇到了相同的值,那么删掉其中一个即可,然后继续遍历,具体细节见代码

三、本题解析所用的编程语言

c++

四、完整且正确的编程代码

/**
 * struct ListNode {
 *  int val;
 *  struct ListNode *next;
 *  ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @return ListNode类
     */
    ListNode* deleteDuplicates(ListNode* head) {
        // write code here
        if (!head)
            return head;
        ListNode* p = head;
        while (p->next) {
            if (p->val == p->next->val) {
                p->next = p->next->next;
            } else {
                p = p->next;
            }
        }
        return head;
    }
};