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++) {
 9            node2 = node2.next;
10        }
11        if(node2.next == null) {   //要加上这一段 要是node2刚好到tail 下面代码无法更新node1, 因为node1在head
12            return head.next;
13        }
14        while(node2.next != null) {
15            p = node1;
16            node1 = node1.next;
17            node2 = node2.next;
18        }
19        p.next = node1.next;
20        return head;
21    }
22 }

原文地址:https://www.cnblogs.com/goPanama/p/9496015.html

时间: 2024-08-30 03:17:18

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

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

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. 思路:遍历一次若链表长度

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