Skip to content

Commit 3acd44f

Browse files
committed
添加py,注释js
1 parent eeda65c commit 3acd44f

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

animation-simulation/链表篇/剑指offer22倒数第k个节点.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,43 @@ public:
9696
JS Code:
9797
```javascript
9898
var getKthFromEnd = function(head, k) {
99+
//特殊情况
99100
if(!head) return head;
101+
//初始化两个指针, 定义指针指向
100102
let pro = head, after = head;
103+
//先移动绿指针到指定位置
101104
for(let i = 0; i < k - 1; i++){
102105
pro = pro.next;
103106
}
107+
//两个指针同时移动
104108
while(pro.next){
105109
pro = pro.next;
106110
after = after.next;
107111
}
112+
//返回倒数第k个节点
108113
return after;
109114
};
110-
```
115+
```
116+
117+
Python Code:
118+
119+
```python
120+
class Solution:
121+
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
122+
# 特殊情况
123+
if head is None:
124+
return head
125+
# 初始化两个指针, 定义指针指向
126+
pro = head
127+
after = head
128+
# 先移动绿指针到指定位置
129+
for _ in range(k - 1):
130+
pro = pro.next
131+
# 两个指针同时移动
132+
while pro.next is not None:
133+
pro = pro.next
134+
after = after.next
135+
# 返回倒数第k个节点
136+
return after
137+
```
138+

0 commit comments

Comments
 (0)