首页 > 其他 > 详细

LeetCode 83. Remove Duplicates from Sorted List

时间:2018-10-29 22:29:05      阅读:147      评论:0      收藏:0      [点我收藏+]

分析

难度 易

来源

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

题目

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3 

解答

 1 package LeetCode;
 2 
 3 /**
 4  * Definition for singly-linked list.
 5  * public class ListNode {
 6  *     int val;
 7  *     ListNode next;
 8  *     ListNode(int x) { val = x; }
 9  * }
10  */
11 public class L83_RemoveDuplicatesFromSortedList {
12     public ListNode deleteDuplicates(ListNode head) {
13         if(head==null)
14             return head;
15         ListNode cur=head;
16         //ListNode temp;
17         while(cur.next!=null){
18             if(cur.val!=cur.next.val)
19                 cur=cur.next;
20             else{
21                 /*temp=cur.next.next;
22                 cur.next=temp;*/
23                 cur.next=cur.next.next;
24             }
25         }
26         return head;
27     }
28 }

 

 

LeetCode 83. Remove Duplicates from Sorted List

原文:https://www.cnblogs.com/flowingfog/p/9873786.html

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