[leetcode-82]Remove Duplicates from Sorted List II(C)
来源:互联网 发布:单片机无线抢答器 编辑:程序博客网 时间:2024/05/19 22:48
问题描述:
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
分析:本来以为是很简单的一个问题,结果发现轻敌了,这里面我之前没有思考好每个循环之间的关系。。
从链表头部开始出发,开始判断头部相同值的个数。当为1时,是我们所需要的节点。当》1时,继续循环。也就是当循环退出时,那一定是一个有效的节点。这里面,需要再对head重新赋值。即使它没变,也重新赋值,不然的话就太乱了。
按照我的思路,就是找到所有满足条件的节点并串联起来。
代码如下:4ms
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ int count(struct ListNode **head){ int count = 1; while(*head && (*head)->next && (*head)->next->val == (*head)->val){ count++; *head = (*head)->next; } return count; }struct ListNode* deleteDuplicates(struct ListNode* head) { if(!head) return head; struct ListNode *prev = NULL; struct ListNode *current = head; struct ListNode *res = head; while(current){ while(current!=NULL && count(¤t)>1){ if(current) current = current->next; } if(!prev){ head = current; }else prev->next = current; prev = current; if(current) current = current->next; } return head;}
0 0
- 【leetcode c++】82 Remove Duplicates from Sorted List II
- [leetcode-82]Remove Duplicates from Sorted List II(C)
- [C语言][LeetCode][82]Remove Duplicates from Sorted List II
- [C++]LeetCode: 6 Remove Duplicates from Sorted List II
- [leetcode](Remove Duplicates from Sorted List II C语言实现)
- 【LeetCode】 82. Remove Duplicates from Sorted List II C语言
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- [leetcode]Remove Duplicates from Sorted List II
- LeetCode-Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- LeetCode - Remove Duplicates from Sorted List II
- 基于Spring + Spring MVC + Mybatis 高性能web构建
- item属性总结
- 常见 Java 字节码 指令 助记符
- 通过设备指针调用其他驱动程序(同步方式)
- 协程框架的堆栈大小陷阱
- [leetcode-82]Remove Duplicates from Sorted List II(C)
- ShaderLab学习笔记
- 在VS中测试Lua项目
- HDU 2191(悼念512汶川大地震遇难同胞——珍惜现在,感恩生活)多重背包
- android 中 setTextColor 方法使用注意事项
- C++中堆栈,堆和栈的区别
- IOS界面适配一: Pt与Px的关系
- Hive安装及基础知识总结
- Object源码研究2——getClass()方法分析