Easy-题目17:83. Remove Duplicates from Sorted List

题目原文:
Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
题目大意:
给出一个排序的单链表,删除所有重复元素使得每个元素仅出现一次。
题目分析:
判断当前节点的值是否等于其后继节点,若等于则删除后继节点(p->next=p->next->next),否则当前节点向右移动(p=p->next)。
源码:(language:c)

struct ListNode* deleteDuplicates(struct ListNode* head) {
    struct ListNode* p = head;
    if(!head)
        return;
    while(p->next) {
        if(p->val == p->next->val)
            p->next=p->next->next;
        else
            p=p->next;
    }
    return head;
}

成绩:
4ms,beats 10.82%,众数4ms,89.18%

你可能感兴趣的:(Easy-题目17:83. Remove Duplicates from Sorted List)