我的思路是直接遍历链表,遇到相同元素直接忽略
先对链表进行判断,如果为空就直接返回空
如果不为空再遍历链表进行逻辑判断
public class Solution {
public ListNode deleteDuplicates (ListNode head) {
//空链表 fast-template
if(head == null)
return null;
//遍历指针
ListNode cur = head;
//指针当前和下一位不为空
while(cur != null && cur.next != null){
//如果当前与下一位相等则忽略下一位
if(cur.val == cur.next.val)
cur.next = cur.next.next;
//否则指针正常遍历
else
cur = cur.next;
}
return head;
}
}