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

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

Open
conan1992 opened this issue Mar 24, 2020 · 0 comments
Open

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

conan1992 opened this issue Mar 24, 2020 · 0 comments
Labels

Comments

@conan1992
Copy link
Owner

题目描述

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

输入: 1->1->2
输出: 1->2

示例 2:

输入: 1->1->2->3->3
输出: 1->2->3

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
    let start = NaN;
    let ans = new ListNode();
    let next = head
    let result = ans
    while( next ){
        if( start != next.val ){
            start = next.val;
            result.next = new ListNode();
            result.next.val = start;
            result = result.next;
        }
        next = next.next;
    }
    return ans.next
};
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