eliminate the duplicates in the sorted list (2)
来源:互联网 发布:软件开发书籍推荐 编辑:程序博客网 时间:2024/06/06 16:49
Question:
Given a sorted list, and remove the duplicates in the list. For example, 1 -> 2 -> 2 -> 3, after removing the duplicates, we have 1 -> 2 -> 3
Analyze:
when we remove the duplicates, we consider all the duplicates as a composite node, and we remove the duplicates from the composite node and update the "next" value of the first node in the composite node.
Code:
public static Node removeDuplication(Node head) {if (head == null || head.next == null ) return head;Node listHead = head; while (head != null) {// if true, we will remove the nodes in the composite node// and only keep the first node in the composite nodeif (head.next != null && head.value == head.next.value) {// the head of the composite nodeNode headComposite = head;while (head.next != null && head.value == head.next.value) {head = head.next;}//head is the first node in the next composite nodehead = head.next;//update the next valueheadComposite.next = head;} else {head = head.next;}}return listHead;}转载请注明出处:http://blog.csdn.net/beiyeqingteng
- eliminate the duplicates in the sorted list (2)
- eliminate the duplicates in the sorted list (2)
- eliminate the duplicates in a sorted linked list (1)
- eliminate the duplicates in a sorted linked list (1)
- HDU 4115 Eliminate the Conflict 2-sat
- HDU 4115 Eliminate the Conflict 2-sat
- HDU_4115 Eliminate the Conflict 2-Sat
- HDOJ4115-Eliminate the Conflict,2SAT
- HDU 4115 Eliminate the Conflict (2-SAT)
- HDU 4115 Eliminate the Conflict (2-SAT)
- 【HDU】4115 Eliminate the Conflict 2-sat
- HDU 4115 Eliminate the Conflict(2-sat)
- HDU 4115 Eliminate the Conflict【2-sat】
- HDU - 4115 Eliminate the Conflict(2-SAT)
- HDU 4115 Eliminate the Conflict 2-SAT
- hdu4115 Eliminate the Conflict--2-sat
- HDU 4115Eliminate the Conflict 2-sat
- hdu 4115 Eliminate the Conflict (2-sat)
- HDU 2845 最大非连续子段和 二维dp
- 内存管理(一)
- 使用指针来传递对象的例子对比
- Java 使用dom读取XML文件及对中文字符的支持
- 安装MYSLQ数据库异常cannot create windows service for mysql.error:0
- eliminate the duplicates in the sorted list (2)
- ssh 启动服务时加载数据
- 中兴ZTE制造,某款Firefox OS原型机曝光。
- vc++建立web中ActiveX插件的过程
- eliminate the duplicates in a sorted linked list (1)
- Manifolds with singularities
- SAP HANA 评估测试(SAP HANA Exam Test)
- Android中集成第三方软件包(.jar, .so)
- 使用nginx后如何在web应用中获取用户ip及原理解释