LeetCode 83. Remove Duplicates from Sorted List(从有序链表中删除重复节点)

时间:2023-03-09 17:36:07
LeetCode 83. Remove Duplicates from Sorted List(从有序链表中删除重复节点)

题意:从有序链表中删除重复节点。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == NULL || head -> next == NULL){
return head;
}
if(head -> val == head -> next -> val){
return deleteDuplicates(head -> next);
}
else{
head -> next = deleteDuplicates(head -> next);
return head;
}
}
};