图解分析
当p_current->val==p_next->val
当p_current->val!=p_next->val :p_current指针向前移动
C实现代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *deleteDuplicates(struct ListNode *head){
if(head==NULL) return head;
struct ListNode *p_current=head;
struct ListNode *p_next=head->next;
While(p_current!=NULL&&p_next!=NULL){
if(p_current->val==p_next->val)
p_current->next=p_next->next;
else
p_current=p_next;
p_next=p_next->next;
}
return head;
}
LeetCode实现代码
本文介绍了一种在链表中删除所有重复元素的方法,并提供了C语言实现代码示例。通过遍历链表并比较相邻节点的值来实现,如果两个相邻节点的值相同,则跳过下一个节点;如果不相同,则继续移动当前节点。
695

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



