[LeetCode]Remove Duplicates from Sorted List
题目:给定一个有序的链表。要求去除链表中反复项
算法:遍历链表。通过指针推断反复项,反复节点的删除仅仅须要改变next指向就可以
/** * 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 (null == head) { // empty list return null; } ListNode ptr = head; ListNode dupPtr = head.next; while (null != dupPtr) { while (null!=dupPtr && ptr.val==dupPtr.val) { dupPtr = dupPtr.next; } if (null != dupPtr) { ptr.next = dupPtr; ptr = dupPtr; dupPtr = dupPtr.next; } } ptr.next = null; return head; } }