首页 > 代码库 > [LeetCode]82 Remove Duplicates from Sorted List II
[LeetCode]82 Remove Duplicates from Sorted List II
https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
http://blog.csdn.net/linhuanmars/article/details/24389429
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; // 1 -> 2 -> 2 -> 3 // pre test testnext next ListNode pre = null; ListNode test = head; ListNode testnext = test.next; boolean deletetest = false; ListNode newhead = null; while (test != null) { if (testnext != null && test.val == testnext.val) { // Delete testnext; deletetest = true; ListNode next = testnext.next; test.next = next; testnext.next = null; testnext = next; } else if (deletetest) { // Delete test deletetest = false; if (pre != null) { pre.next = testnext; } test.next = null; test = testnext; if (test != null) testnext = test.next; } else { pre = test; if (newhead == null) newhead = test; test = testnext; if (test != null) testnext = test.next; } } return newhead; } }
[LeetCode]82 Remove Duplicates from Sorted List II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。