这题有两个难度,这是难度一,我按照力扣网站推荐的刷题列表,先做了难度二。再做这个就容易理解了。我就自己写了,也懒得去看答案了。
个人答案:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return head;
}
ListNode dummy = new ListNode(0, head);
ListNode cur = dummy;
while(cur.next!=null && cur.next.next!=null){
if(cur.next.val == cur.next.next.val){
cur.next = cur.next.next;
}else{
cur = cur.next;
}
}
return dummy.next;
}
}


被折叠的 条评论
为什么被折叠?



