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

删除链表的倒数第N个节点 #12

Open
JesseZhao1990 opened this issue Jun 26, 2018 · 0 comments
Open

删除链表的倒数第N个节点 #12

JesseZhao1990 opened this issue Jun 26, 2018 · 0 comments
Labels

Comments

@JesseZhao1990
Copy link
Owner

JesseZhao1990 commented Jun 26, 2018

image

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function(head, n) {
    
    var virtulNode = new ListNode('virtul');
    virtulNode.next = head;
    var p = virtulNode;
    var q = virtulNode;
    
    for(var i=0;i<n+1;i++){
        q=q.next;
    }
    
    while(q!= null){
        p=p.next;
        q=q.next;
    }
    
    var delNode = p.next;
    p.next = delNode.next;
    
    return virtulNode.next;
    
};

leetcode原题链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/

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