题目
- 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。
代码
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function deleteDuplicates(head: ListNode | null): ListNode | null {
const FinallyVal: ListNode = head
while(head && head.next && head.next.next) {
if(head.val === head.next.val) {
head.next = head.next.next
}else {
head = head.next
}
}
if(head && head.next && head.val === head.next.val) {
head.next = null
}
return FinallyVal
};