-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path删除链表的倒数第N个节点.py
46 lines (35 loc) · 1.04 KB
/
删除链表的倒数第N个节点.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def removeNthFromEnd(self, head, n):
# 这个是删除倒数第N个节点
if head.next == None and n == 1:
return None
# 用哈希表记录位置
positions = {}
cur = head
position = 0
while cur.next is not None:
positions[position] = cur
cur = cur.next
position += 1
positions[position] = cur
if n == 1:
positions[position - 1].next = None
return head
# print(positions)
if position - (n- 1) == 0:
return head.next
positions[position - (n- 1) - 1].next = positions[position - (n - 1) + 1]
return head
# print(head.next.val)
# 这个题目用双指针也可以
s1 = ListNode(1)
s2 = ListNode(2)
s3 = ListNode(3)
s1.next =s2
# s2.next =s3
print(Solution().removeNthFromEnd(s1, 1).val)