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 reverse linked list-ii #18

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
28 changes: 28 additions & 0 deletions docs/linked-list/reverse-linked-list-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defaultValue="java"
values={[
{ label: 'Java', value: 'java', },
{ label: 'C++', value: 'cpp', },
{ label: 'Python', value: 'python', },
]
}>
<TabItem value="java">
Expand Down Expand Up @@ -89,5 +90,32 @@ public:
};
```

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

```python
# Reverse Linked List II
# 迭代版,时间复杂度O(n),空间复杂度O(1)
class Solution:
def reverseBetween(self, head, m, n):
dummy = ListNode(-1)
dummy.next = head

prev = dummy;
for _ in range(m-1):
prev = prev.next
head2 = prev

prev = head2.next
cur = prev.next
for i in range(m, n):
prev.next = cur.next
cur.next = head2.next
head2.next = cur # 头插法
cur = prev.next

return dummy.next
```

</TabItem>
</Tabs>