这个题难度不大,但在实现的时候忽略了传入一个空的ListNode的情况,导致开始没能Accept。
实现:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode current = head;
while(current != null) {
if(current.next != null && current.val == current.next.val)
current.next = current.next.next;
else
current = current.next;
}
return head;
}
}
总结:这就是考基本功的,难度不大。