LeetCode 83. Remove Duplicates from Sorted List

Posted flowingfog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 83. Remove Duplicates from Sorted List相关的知识,希望对你有一定的参考价值。

分析

难度 易

来源

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的主要内容,如果未能解决你的问题,请参考以下文章

leetcode83-Remove Duplicates from Sorted List

[LeetCode] 83. Remove Duplicates from Sorted List

LeetCode83 Remove Duplicates from Sorted List

LeetCode 83 Remove Duplicates from Sorted List

LeetCode:83.Remove Duplicates from Sorted List

leetcode83 Remove Duplicates from Sorted List