题目
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:1
2Input: 1->1->2
Output: 1->2
Example 2:1
2Input: 1->1->2->3->3
Output: 1->2->3
分析
这也算一道简单的题目吧,设计的知识有
- 链表节点的申请
- 判断链表空与非空
- 链表结束的判断
C code1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18struct ListNode * p;
p=head;
if(!head)
{
return NULL;
}
while(p&&p->next)
{
if(p->val==p->next->val)
{
p->next=p->next->next;
}
else
{
p=p->next;
}
}
return head;
这次感受还算颇丰,链表的节点判断还是比较重要的一个方面的。