回到技术博客

82. 删除排序链表中的重复元素 II🔖链表

typescript

/**
 * 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 {
  if (!head) return head
  const firstNode = new ListNode(-1, head);
  let curr = firstNode;
  while (curr.next && curr.next.next) {
    if (curr.next.val === curr.next.next.val) {
      const temp = curr.next.val
      while (curr.next && curr.next.val === temp)
        curr.next = curr.next.next;
    } else {
      curr = curr.next;
    }
  }
  return firstNode.next;
};
— 写于生活,留给未来 —

最后更新于 2024-11-17

树下留言

LET’S TALK

文字是一次相遇。很高兴听到你的声音。

评论加载中…