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
.
===================================================================
题目链接:https://leetcode.com/problems/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 ;
while ( head -> next != NULL && head -> val == head -> next -> val )
head = head -> next ;
head -> next = deleteDuplicates ( head -> next ) ;
return head ;
}
};