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

移除链表元素 #144

Open
louzhedong opened this issue Mar 27, 2019 · 0 comments
Open

移除链表元素 #144

louzhedong opened this issue Mar 27, 2019 · 0 comments

Comments

@louzhedong
Copy link
Owner

习题

出处 LeetCode 算法第203题

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

思路

顺序遍历

解答

/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function(head, val) {
    var first = new ListNode();
    first.next = head;
    
    var flag = first;
    
    while(head) {
        if (head.val == val) {
            first.next = head.next;
            head = head.next;
        } else {
            head = head.next;
            first = first.next;
        }
    }
    
    return flag.next;
};
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

1 participant