码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 83. Remove Duplicates from Sorted List

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

标签:remove   cat   null   ini   out   int   style   .com   ems   

分析

难度 易

来源

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

标签:remove   cat   null   ini   out   int   style   .com   ems   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!