【leetcode】83. Remove Duplicates from Sorted List
来源:互联网 发布:手机淘宝好评能修改吗 编辑:程序博客网 时间:2024/06/15 12:52
题目要求:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
思路:用两个指针,一个指向当前结点,一个指向后一个结点,如果当前结点的值等于后一个结点的值,就把后一个节点向后移一位,再去比较;如果不等于,就把两个指针都往后移。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null||head.next==null) { return head; } ListNode cur=head; ListNode next = head.next; while(next!=null) { if(cur.val==next.val) { cur.next=next.next; next = next.next; }else { cur=next; next=next.next; } } return head; }}
0 0
- [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
- 【leetcode】83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List LeetCode
- 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
- 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
- [LeetCode]83. Remove Duplicates from Sorted List
- 17. Letter Combinations of a Phone Number
- SharedPreferences存取特殊数据
- 单片机的最简单的几个编程
- C#_MouseDown事件没反应
- 1.模板方法 Template Method
- 【leetcode】83. Remove Duplicates from Sorted List
- Android 视图拖拽辅助类 ViewDragHelper 的使用
- java实现打印直角三角形
- 今日学习感悟
- ubuntu挂载分区问题
- 算法训练 出现次数最多的整数
- java实现打印倒直角三角形
- c语言语句
- java实现打印等腰三角形