容易错的地方:要判断链表是否为空,为空直接返回
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null)return head;
ListNode newNode = head;
while(newNode.next!=null){
if(newNode.val == newNode.next.val){
newNode.next = newNode.next.next;
}else{
newNode = newNode.next;
}
}
return head;
}
}
网友评论