Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

83. 删除排序链表中的重复元素 #63

Open
Geekhyt opened this issue Aug 24, 2021 · 0 comments
Open

83. 删除排序链表中的重复元素 #63

Geekhyt opened this issue Aug 24, 2021 · 0 comments
Labels

Comments

@Geekhyt
Copy link
Owner

Geekhyt commented Aug 24, 2021

原题链接

1.将 curr 指针指向链表头节点
2.遍历链表,注意边界条件
3.如果当前节点与它后面的节点值相等,则删除它后面与它重复的节点
4.不重复则继续遍历,最后返回头节点

const deleteDuplicates = function(head) {
    let curr = head
    while (curr !== null && curr.next !== null) {
        if (curr.val === curr.next.val) {
            curr.next = curr.next.next
        } else {
            curr = curr.next
        }
    }
    return head
}
  • 时间复杂度: O(n)
  • 空间复杂度: O(1)
@Geekhyt Geekhyt added the 简单 label Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant