/**
* 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 )
return NULL;
else
{
ListNode *deal, *next;
deal = head;
while( deal )
{
next = deal->next;
while( next )
{
if( next->val == deal->val )
next = next->next;
else
break;
}
deal->next = next;
deal = deal->next;
}
return head;
}
}
};Remove Duplicates from Sorted List
最新推荐文章于 2023-01-13 08:10:00 发布
本文介绍了一种在单链表中删除所有重复元素的方法。通过遍历链表并比较相邻节点的值,若发现重复则跳过重复节点,直至链表中不再存在相同值的节点。此方法适用于数据结构与算法的学习。
274

被折叠的 条评论
为什么被折叠?



