LeetCode OJ 83. Remove Duplicates from Sorted List

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

Leet Code OJ 83. Remove Duplicates from Sorted List [Difficulty: Easy]

LeetCode OJ 27. Remove Element

LeetCode OJ_题解(python):027-Remove Element ArrayEasy

LeetCode OJ 203Remove Linked List Elements

LeetCode OJ 26. Remove Duplicates from Sorted Array

LeetCode 83. Remove Duplicates from Sorted List