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

2022-10-13 #76

Open
github-actions bot opened this issue Oct 12, 2022 · 1 comment
Open

2022-10-13 #76

github-actions bot opened this issue Oct 12, 2022 · 1 comment

Comments

@github-actions
Copy link

No description provided.

@gongpeione
Copy link
Contributor

19 Remove Nth Node From End of List

/*
 * @lc app=leetcode id=19 lang=typescript
 *
 * [19] Remove Nth Node From End of List
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
    const dummy = new ListNode(0, head);

    // the distance between left and right is always n
    let left = dummy;
    let right = head;
    for (let i = 0; i < n; i++) {
        right = right.next;
    }

    // right will reach the end of this list
    // and left would be the prev node of target node that we need to delete
    while (right) {
        left = left.next;
        right = right.next;
    }

    // delete the node
    left.next = left.next.next;

    return dummy.next;
};
// @lc code=end

Nickname: Geeku
From vscode-hzfe-algorithms

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

No branches or pull requests

1 participant