文章目录
- 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。 https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
- 双指针技巧,快慢指针。
- 提交Code # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: fast = head slow = head while(n >= 0): if(fast == None): head = head.next return head fast = fast.next n -= 1 while( fast != None): fast = fast.next slow = slow.next slow.next = slow.next.next return head 完整Code class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def removeNthFromEnd(self, head:ListNode,n:int): listN = ListNode(head[0]) tmp = listN # 环状链表的构造 for m in range(1,head.__len__()): listN.next = ListNode(head[m]) listN = listN.next print('-----------------------') fast = tmp slow = tmp # if(n == 5): # tmp = tmp.next # return tmp while (n >= 0): # 用于判断是否为移除【正数】第一个数字的情况 if(fast == None): tmp = tmp.next return tmp fast = fast.next n -= 1 print('888888888888888888888888888888') while (fast != None): fast = fast.next slow = slow.next slow.next = slow.next.next print('888888888888888888888888888888') return tmp head = [1,2] S = Solution() print("###################result###################") result = S.removeNthFromEnd(head,1) print("result"+str(result)) print('-------------------------------------------') while result != None: print(result.val) result = result.next
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

双指针技巧,快慢指针。
双指针技巧,快慢指针。
- 提交Code
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
fast = head
slow = head
while(n >= 0):
if(fast == None):
head = head.next
return head
fast = fast.next
n -= 1
while( fast != None):
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return head
- 完整Code
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def removeNthFromEnd(self, head:ListNode,n:int):
listN = ListNode(head[0])
tmp = listN
# 环状链表的构造
for m in range(1,head.__len__()):
listN.next = ListNode(head[m])
listN = listN.next
print('-----------------------')
fast = tmp
slow = tmp
# if(n == 5):
# tmp = tmp.next
# return tmp
while (n >= 0):
# 用于判断是否为移除【正数】第一个数字的情况
if(fast == None):
tmp = tmp.next
return tmp
fast = fast.next
n -= 1
print('888888888888888888888888888888')
while (fast != None):
fast = fast.next
slow = slow.next
slow.next = slow.next.next
print('888888888888888888888888888888')
return tmp
head = [1,2]
S = Solution()
print("###################result###################")
result = S.removeNthFromEnd(head,1)
print("result"+str(result))
print('-------------------------------------------')
while result != None:
print(result.val)
result = result.next
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
fast = head
slow = head
while(n >= 0):
if(fast == None):
head = head.next
return head
fast = fast.next
n -= 1
while( fast != None):
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return head
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def removeNthFromEnd(self, head:ListNode,n:int):
listN = ListNode(head[0])
tmp = listN
# 环状链表的构造
for m in range(1,head.__len__()):
listN.next = ListNode(head[m])
listN = listN.next
print('-----------------------')
fast = tmp
slow = tmp
# if(n == 5):
# tmp = tmp.next
# return tmp
while (n >= 0):
# 用于判断是否为移除【正数】第一个数字的情况
if(fast == None):
tmp = tmp.next
return tmp
fast = fast.next
n -= 1
print('888888888888888888888888888888')
while (fast != None):
fast = fast.next
slow = slow.next
slow.next = slow.next.next
print('888888888888888888888888888888')
return tmp
head = [1,2]
S = Solution()
print("###################result###################")
result = S.removeNthFromEnd(head,1)
print("result"+str(result))
print('-------------------------------------------')
while result != None:
print(result.val)
result = result.next
