19. Remove Nth Node From End of List(js)

19. Remove Nth Node From End of List

Given a linked list, remove the n-th node from the end of list and return its head.

Example:

Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.题意:删除链表的倒数第n个节点代码如下:
/**
 * 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) {
     if(head.next==null) return null;
        //定义两个指针
        let pre=head,cur=head;
        //让cur沿着head向后移动n个节点
        for(let i=0;i<n;i++){
            cur=cur.next;
        }
        if(!cur) return head.next;
        //直至cur指向head末端
        while(cur.next){
            cur=cur.next;
            pre=pre.next;
        }
        pre.next=pre.next.next;
        return head;
};

原文地址:https://www.cnblogs.com/xingguozhiming/p/10387351.html

时间: 2024-08-26 12:51:42

19. Remove Nth Node From End of List(js)的相关文章

leetcode 19. Remove Nth Node From End of List(链表)

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

19. Remove Nth Node From End of List(medium)

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. 思路:遍历一次若链表长度

19. Remove Nth Node From End of List (LL)

1 class Solution { 2 public ListNode removeNthFromEnd(ListNode head, int n) { 3 if(head == null) return head; 4 if(head.next == null) return null; 5 ListNode node1 = head; 6 ListNode node2 = head; 7 ListNode p = node1; 8 for(int i = 0; i < n - 1; i++

Leetcode 19. Remove Nth Node From End of List(python)

链表操作,只能遍历一遍然后. 用双指针 # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int

leetcode_19题——Remove Nth Node From End of List(链表)

Remove Nth Node From End of List Total Accepted: 54129 Total Submissions: 197759My Submissions Question Solution Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5,

【leetcode】Remove Nth Node From End of List(easy)

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. 思路: 最基本的思路肯定

Remove Nth Node From End of List(链表)

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->next这个链表删除倒数第N个节点”,将head的next指针指向该子问题的结果,返回head即可.这个方法时间复杂度高,不推荐. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;

[Leetcode][Python]19: Remove Nth Node From End of List

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 19: Remove Nth Node From End of Listhttps://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ Given a linked list, remove the nth node from the end of list and return its head. For ex