首页 > 编程语言 > 详细

leetcode 83. 删除排序链表中的重复元素

时间:2019-11-29 18:31:20      阅读:70      评论:0      收藏:0      [点我收藏+]

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

输入: 1->1->2
输出: 1->2
示例 2:

输入: 1->1->2->3->3
输出: 1->2->3

 1 class Solution {
 2     public ListNode deleteDuplicates(ListNode head) {
 3         if(head==null){
 4             return head;
 5         }
 6         ListNode iterator = head.next;
 7         ListNode pre = head;
 8         ListNode next = null;
 9         int rep = head.val;
10         while(iterator!=null){
11             next=iterator.next;
12             if(iterator.val==rep){
13                 pre.next=next;
14             }else{
15                 rep=iterator.val;
16                 pre=iterator;
17             }
18             iterator=next;
19         }
20         return head;
21     }
22 }

 

leetcode 83. 删除排序链表中的重复元素

原文:https://www.cnblogs.com/gongzixiaobaibcy/p/11959231.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!