题目:
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.
分析:
在一个链表中,将重复val的node去掉
思路:
遍历,改变那些val相同的node的指针的指向。
代码:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* l = head;
ListNode* l1 = l;
if(head)
{
head = head -> next;
while(head)
{
if(head->val != l->val)
{
l = head;
l -> next = head -> next;
}
else
{
l->next = head -> next;
}
head = head -> next;
}
}
return l1;
}