LeetCode 82. Remove Duplicates from Sorted List II JavaScript

睡覺的時候聽到外面有聲音都會大驚Q
var deleteDuplicates = function(head) {
const newList = new ListNode();
newList.next = head;
let node = newList;
// the last known distinct node
while (node.next) {
if (node.next.val === node.next?.next?.val) {
// if the next two nodes are equal...
let nonValNode = node.next.next.next;
while (nonValNode && nonValNode.val === node.next.val) {
// ...find the first one that isn't...
nonValNode = nonValNode.next;
}
node.next = nonValNode;
// ...and glue it to the last known distinct node;...
} else {
node = node.next;
// ...otherwise the next node is distinct
}
}
return newList.next;
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
黃韋智

黃韋智

自學寫程式,目前爲 React 前端工程師。熱愛旅遊,將近 30 個國家,足跡遍佈亞洲與歐洲。生命與街舞已經離不開,歡迎訂閱 Youtube 頻道:https://www.youtube.com/channel/UCEU-bEDl7R-iGyLVZFae33g