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

删除排序链表中的重复元素 #284

Open
Sunny-117 opened this issue Nov 8, 2022 · 5 comments
Open

删除排序链表中的重复元素 #284

Sunny-117 opened this issue Nov 8, 2022 · 5 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@lxy-Jason
Copy link
Contributor

var deleteDuplicates = function(head) {
    let temp = head;
    while(temp && temp.next){
        if(temp.val === temp.next.val){
            let next = temp.next
            temp.next = next.next
        }
        else{
            temp = temp.next
        }
    }
    return head
};

@lxy-Jason
Copy link
Contributor

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

var deleteDuplicates = function (head) {
    let dummyHead = new ListNode(0, head); 
    let prev = dummyHead;
    let cur = head;
    while (cur && cur.next) {
        if (cur.val === cur.next.val) { //值相同时
            let val = cur.val;
            while (cur && cur.val === val) { //找到不同值为止
                cur = cur.next
            }
            prev.next = cur; //前指针直接指向重复值之后的节点
        }
        else {
            prev = cur; //注意顺序
            cur = cur.next;

        }
    }
    return dummyHead.next;
};

@Nasuke
Copy link
Contributor

Nasuke commented Nov 20, 2022

删除链表中重复元素II

var deleteDuplicates = function(head) {
    let ret = new ListNode(null, head), p = ret
    if(!head) return head
    let arr = []
    while(p.next) {
        if(p.val == p.next.val) {
            arr.push(p.val)
        }
         p = p.next
    }
    p = ret 
    while(p.next){
        if(arr.includes(p.next.val)){
            p.next = p.next.next
        }else {
            p = p.next
        }
    }
    return ret.next

};

@Pcjmy
Copy link
Contributor

Pcjmy commented Feb 23, 2023

题目链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-list/description
时间复杂度:O(n)

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
    let last=head;
    let node=head;
    if(node)
    {
        node=node.next;
        while(node)
        {
            if(node.val!=last.val)
            {
                last.next=node;
                last=node;
            }
            node=node.next;
        }
        last.next=null;
    }
    return head;
};

@Bbbtt04
Copy link

Bbbtt04 commented Apr 12, 2023

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
    let p = head;
    while(p && p.next) {
        if (p.val === p.next.val) {
            let next = p.next.next;
            p.next = next;
        } else {
            p = p.next;
        }
    }
    return head;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants