[LeetCode]Remove Duplicates from Sorted List
2014-07-02 20:20
274 查看
题目:给定一个有序的链表,要求去除链表中重复项
算法:遍历链表,通过指针判断重复项,重复节点的删除只需要改变next指向即可
算法:遍历链表,通过指针判断重复项,重复节点的删除只需要改变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; } }
相关文章推荐
- leetcode 38: Remove Duplicates from Sorted List II
- LeetCode - Remove Duplicates from Sorted List
- leetcode 37: Remove Duplicates from Sorted List
- LeetCode_Remove Duplicates from Sorted List II
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List
- Leetcode: Remove Duplicates from Sorted List
- leetcode 38: Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List
- [LeetCode] Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted List
- [LeetCode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II, Solution
- [LeetCode] Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode_Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List