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
.
分析:
题目大意为将给定的已排序好的数组删除重复的数组,若当前的地址的值与它所指的地址的值是相同的,则将这个地址的指针指向下下个地址
/**
* 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) {
ListNode *l=head;
while(head==NULL) return head;
while(l!=NULL&&l->next!=NULL){
if(l->val==l->next->val){
l->next=l->next->next;
}else{
l=l->next;
}
}
return head;
}
};