#-*- coding: UTF-8 -*-
#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点
# 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
"""
dummy=ListNode(0)
dummy.next=head
p=dummy
q=dummy
for i in range(n):
q=q.next
while q.next:
p=p.next
q=q.next
rec=p.next
p.next=rec.next
del rec
return dummy.next
相关文章
- 02-2419.Remove Nth Node From End of List(List; Two-Pointers)
- 02-24【leetcode❤python】83. Remove Duplicates from Sorted List
- 02-24【leetcode❤python】 19. Remove Nth Node From End of List
- 02-24LeetCode OJ 292.Nim Gam19. Remove Nth Node From End of List
- 02-2425.Remove Nth Node From End of List(删除链表的倒数第n个节点)
- 02-24Leetcode_19_Remove Nth Node From End of List
- 02-2463. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
- 02-24[leetcode]Remove Duplicates from Sorted List II @ Python
- 02-24[Leetcode]-- Remove Nth Node From End of List
- 02-2419. Remove Nth Node From End of List