添加py,注释js

pull/33/head
jaredliw 2021-07-12 17:08:45 +08:00
parent eeda65c2d3
commit 3acd44f653
1 changed files with 29 additions and 1 deletions

View File

@ -96,15 +96,43 @@ public:
JS Code:
```javascript
var getKthFromEnd = function(head, k) {
//特殊情况
if(!head) return head;
//初始化两个指针, 定义指针指向
let pro = head, after = head;
//先移动绿指针到指定位置
for(let i = 0; i < k - 1; i++){
pro = pro.next;
}
//两个指针同时移动
while(pro.next){
pro = pro.next;
after = after.next;
}
//返回倒数第k个节点
return after;
};
```
```
Python Code:
```python
class Solution:
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
#
if head is None:
return head
# ,
pro = head
after = head
# 绿
for _ in range(k - 1):
pro = pro.next
#
while pro.next is not None:
pro = pro.next
after = after.next
# k
return after
```