|
| 1 | +--- |
| 2 | +title: Sword finger Offer II 021. Delete the countdown of the linked list n Node.md |
| 3 | +date: '2024.01.01 0:00' |
| 4 | +tags: |
| 5 | + - - Python |
| 6 | + - - answer |
| 7 | +abbrlink: 3ed2f01c |
| 8 | +--- |
| 9 | + |
| 10 | +# topic: |
| 11 | + |
| 12 | +[Sword finger Offer II 021. Delete the countdown of the linked list n Node.md](https://leetcode.cn/problems/SLwz0R/description/) |
| 13 | + |
| 14 | +# Thought: |
| 15 | +Double pointer(Sliding window algorithm)。 |
| 16 | +In this method,We first created a virtual head node dummy,And point it to the original head point head。 |
| 17 | +Then we use two pointers fast and slow,Will fast Poor movement move forward n step。 |
| 18 | +Next,We move at the same time fast and slow pointer,until fast pointer到达链表的末尾。 |
| 19 | +at this time,slow pointer指向倒数第 n+1 Node,我们Will其 next pointer指向 slow.next.next,So as to delete the countdown n Node。 |
| 20 | + |
| 21 | +at last,We return virtual head nodes next pointer,It points to delete the countdown n Node后的链表的Head node。 |
| 22 | + |
| 23 | +At the beginning, according toCLinked |
| 24 | +# Code: |
| 25 | + |
| 26 | +```python Sliding window algorithm |
| 27 | +class Solution: |
| 28 | + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: |
| 29 | + # Create a virtual head node |
| 30 | + dummy = ListNode(0) |
| 31 | + # Will虚拟Head node的 next Pointing to the original head point |
| 32 | + dummy.next = head |
| 33 | + # 定义快慢pointer,并Will快Poor movement move forward n step |
| 34 | + fast = slow = dummy |
| 35 | + for i in range(n): |
| 36 | + fast = fast.next |
| 37 | + # 同时移动快慢pointer,until快pointer到达链表末尾 |
| 38 | + while fast and fast.next: |
| 39 | + fast = fast.next |
| 40 | + slow = slow.next |
| 41 | + |
| 42 | + # Will慢pointer的 next pointer指向慢pointer的下一Node的下一Node,So as to delete the countdown n Node |
| 43 | + slow.next = slow.next.next |
| 44 | + return dummy.next |
| 45 | +``` |
| 46 | + |
| 47 | +```python Pure linked list |
| 48 | +class Solution: |
| 49 | + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: |
| 50 | + |
| 51 | + # Calculation length |
| 52 | + def get_list_length(head): |
| 53 | + # If the linked list is empty,Length0 |
| 54 | + if not head: |
| 55 | + return 0 |
| 56 | + |
| 57 | + # Links in traversal,Count |
| 58 | + length = 0 |
| 59 | + current = head |
| 60 | + while current: |
| 61 | + length += 1 |
| 62 | + current = current.next |
| 63 | + |
| 64 | + return length |
| 65 | + |
| 66 | + # Find the deleted node |
| 67 | + def delete(node, count): |
| 68 | + if count == n + 1 or n == length: |
| 69 | + node.next = node.next.next |
| 70 | + return |
| 71 | + if node.next: |
| 72 | + delete(node.next, count - 1) |
| 73 | + |
| 74 | + length = get_list_length(head) |
| 75 | + delete(head, length) |
| 76 | + return head |
| 77 | + |
| 78 | + |
| 79 | +def list_to_linked_list(lst): |
| 80 | + if not lst: |
| 81 | + return None |
| 82 | + |
| 83 | + # Head node |
| 84 | + head = ListNode(lst[0]) |
| 85 | + current = head |
| 86 | + |
| 87 | + # Elements in the list of traversal,Will其转换为链表节点 |
| 88 | + for i in range(1, len(lst)): |
| 89 | + current.next = ListNode(lst[i]) |
| 90 | + current = current.next |
| 91 | + |
| 92 | + return head |
| 93 | +``` |
0 commit comments