题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode deleteDuplicates (ListNode head) {
if(head == null || head.next == null) return head ;
ListNode pre = new ListNode(1001) ;
pre.next = head ;
ListNode slow_pre = pre ;
ListNode slow = head ;
ListNode fast = head.next ;
while(fast != null) {
if(fast.val == slow.val) {
fast = fast.next ;
if(fast == null || fast.val != slow.val) {
slow_pre.next = fast ;
slow = fast ;
if(fast != null) {
fast = fast.next ;
}
}
} else {
slow_pre = slow ;
slow = slow.next ;
fast = fast.next ;
}
}
return pre.next ;
}
}
一个菜鸟的算法刷题记录 文章被收录于专栏
分享一个菜鸟的成长记录
查看16道真题和解析