收获:
1.在python中对链表中节点进行操作时:
a) 可以先记录头节点 a = head
b) 然后再操作头节点, head = head.next
c) 这样最后可以直接返回 a
代码1) 用了两次遍历的方法
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
tail = head
count = 0
while tail :
tail = tail.next
count +=1
print(n)
n = count - n
print(n)
if n ==0:
return head.next
else:
a = head
m = n-1
point = 0
while point < m:
head = head.next
point +=1
target = head.next
head.next = target.next
target.next = None
return a