题解 | #牛群排列去重#
判断是否为null,直接返回
不能null,那么就遍历,获取下一节点,与当前节点比较,如果值相同,那么以下一节点为起始点,往下找不等于当前节点的值的指针
如果找到,当前节点下一节点指向找到的节点,当前节点指向找到的节点继续,直到为null
public ListNode deleteDuplicates (ListNode head) {
// write code here
if (head == null) {
return null;
}
ListNode tem = head;
while (head != null && head.next != null) {
if (head.val == head.next.val) {
head.next = head.next.next;
} else {
head = head.next;
}
}
return tem;
}
#链表##牛群排列去重#