83.Remove Duplicates from Sorted List
2018-03-07 15:59
323 查看
83.Remove Duplicates from Sorted List
题目
给出一有序链表,删除所有重复的元素使每个元素仅出现一次。比如:
给出 1->1->2,返回 1->2.
给出1->1->2->3->3,返回1->2->3.
代码块
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode deleteDuplicates(ListNode head) { if(head == null) return null; ListNode left = head; ListNode right = head.next; while(right != null){ if(left.val == right.val){ right = right.next; }else{ left.next = right; left = right; } } left.next = null; return head; } }
代码分析
这个题目使用了两个指针,如果两个数相同,就移动后面一个指针;否则,将两个指针同时右移。相关文章推荐
- leetcode(83)—— 删除已排序链表重复元素(Remove Duplicates from Sorted List)
- 【leetcode】【单链表】【83】Remove Duplicates from Sorted List
- LeetCode(83)Remove Duplicates from Sorted List
- Leetcode-83 Remove Duplicates from Sorted List
- Leetcode 83 Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List*
- 83. Remove Duplicates from Sorted List
- LeetCode 83 Remove Duplicates from Sorted List II
- [83] Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- [leedcode 83] Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- LeetCode83之Remove Duplicates from Sorted List的Java题解
- 83. Remove Duplicates from Sorted List
- LeetCode 83:Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- [leetcode 83]Remove Duplicates from Sorted List
- [leetcode][83]Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List