首页 > 代码库 > LeetCode OJ 83. Remove Duplicates from Sorted List

LeetCode OJ 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.

 

Subscribe to see which companies asked this question

解答
这题太水了,注意不要对NULL解引用就好了。
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    struct ListNode *pNode = head;
    
    while(NULL != pNode&&NULL != pNode->next){
        if(pNode->val == pNode->next->val){
            pNode->next = pNode->next->next;
        }
        else{
            pNode = pNode->next;
        }
    }
    return head;
}

 

LeetCode OJ 83. Remove Duplicates from Sorted List