美文网首页
leetcode-82-删除链表中重复元素2

leetcode-82-删除链表中重复元素2

作者: kayleeWei | 来源:发表于2021-03-30 01:27 被阅读0次

var deleteDuplicates = function(head) {
var preHead = new ListNode(-1);
preHead.next = head;

var pre = preHead;
var cur = head;

while(cur !== null && cur.next !== null) {
  if (pre.next.val !== cur.next.val) {
    pre = pre.next;
    cur = cur.next;
  } else {
    while (cur !== null && cur.next !== null && pre.next.val === cur.next.val) {
      cur = cur.next
    }
    pre.next = cur.next;
    cur = cur.next; 
  }
}

return preHead.next;

};

相关文章

网友评论

      本文标题:leetcode-82-删除链表中重复元素2

      本文链接:https://www.haomeiwen.com/subject/ehqwhltx.html