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
        :rtype: ListNode
        """
        left=right=head
        for i in range(n):
            right=right.next
        if right is None:   return head.next    #special
        while right.next is not None:
            right=right.next
            left=left.next

        left.next=left.next.next
        return head

  

时间: 2024-08-29 22:21:13

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

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(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】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. 思路: 最基本的思路肯定

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(删除链表中倒数第N个节点)

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

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 链表

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 lis

leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法

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

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,