Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added python code remove nth node from end of list #17

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/linked-list/remove-nth-node-from-end-of-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defaultValue="java"
values={[
{ label: 'Java', value: 'java', },
{ label: 'C++', value: 'cpp', },
{ label: 'Python', value: 'python', },
]
}>
<TabItem value="java">
Expand Down Expand Up @@ -82,6 +83,28 @@ public:
};
```

</TabItem>
<TabItem value="python">

```python
# Remove Nth Node From End of List
# 时间复杂度O(n),空间复杂度O(1)
class Solution:
def removeNthFromEnd(self, head, n):
dummy = ListNode(-1)
dummy.next = head
p, q = dummy, dummy

for i in range(n): # q先走n步
q = q.next

while q.next != None: # 一起走
p = p.next
q = q.next
p.next = p.next.next
return dummy.next
```

</TabItem>
</Tabs>

Expand Down