p83

LeetCode p83 Remove Duplicates from Sorted List 题解

1.题目:

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.

题意:

去掉一个有序链表中重复的数。

2.解题思路:

见代码

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
/**
* 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) {
ListNode ans = head;
if (head == null)
return null;
while (head != null && head.next != null) {
if (head.next.val == head.val) {
head.next = head.next.next;
continue;
}
head = head.next;
}
return ans;
}
}


4.一些总结: