题解 | #删除有序链表中重复的元素-I#
删除有序链表中重复的元素-I
http://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79
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(101) ; pre.next = head ; ListNode slow = pre ; ListNode fast = head ; while(fast != null) { if(fast.val == slow.val) { fast = fast.next ; if(fast == null || fast.val != slow.val) { slow.next = fast ; } } else { slow = slow.next ; fast = fast.next ; } } return pre.next ; } }
一个菜鸟的算法刷题记录 文章被收录于专栏
分享一个菜鸟的成长记录