leetcode 83. Remove Duplicates from Sorted List
来源:互联网 发布:淘宝直通车退款 编辑:程序博客网 时间:2024/04/29 17:12
题目内容
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.
题目分析
删除已排序链表中重复出现的结点,所有的结点出现一次。
每次选取两个结点,前一个结点prev和下一个结点,temp=prev.next 。然后比较两个结点的值,如果两个结点的值相等,则prev指向temp的下一个结点,temp等于其下一个结点。
如果不想等,则prev=temp,temp=temp.next。
/** * 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) return head; ListNode prev=head; ListNode temp=head.next; while(temp!=null) { if(prev.val==temp.val) { prev.next=temp.next;//结点指向发生变化。 temp=temp.next; } else { prev=temp; temp=temp.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
- sizeof和sizeof(string)的问题
- c#后台将数据转换为json字符串,前台ajax解析json字符串
- PythonChallenge Mission 9
- nodeJs--模块module.exports与实例化方法
- python - __dict__的妙用
- leetcode 83. Remove Duplicates from Sorted List
- ZooKeeper数据模型、命名空间以及节点的概念
- 子div在父div居底
- 机器学习:文本挖掘之特征选择
- HDU.1027 Ignatius and the Princess II【全排列:关于next_permutation问题】(3.14)
- [BZOJ 2176]Strange string
- 顺时针旋转矩阵
- vector--resize
- coordinatorLayout 的一个小效果