Skip to content

[LeetCode] 83. Remove Duplicates from Sorted List #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Animenzzzz opened this issue Jul 14, 2019 · 0 comments
Open

[LeetCode] 83. Remove Duplicates from Sorted List #8

Animenzzzz opened this issue Jul 14, 2019 · 0 comments

Comments

@Animenzzzz
Copy link
Owner

Description

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

解题思路:这题比较简单,一层遍历,如果下一个值相同就直接将其指向下下一个值,否则头结点下移一位

C解题:

struct ListNode* deleteDuplicates(struct ListNode* head){
    struct ListNode* orign_head1 = head;
    while (head)
    {
        if(!head->next) break;
        if (head->val == head->next->val)
        {
            head->next = head->next->next;
        }else{
            head = head->next;
        }
    }
    return orign_head1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant