mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-11-28 06:48:53 +00:00
添加py,注释js
This commit is contained in:
parent
eeda65c2d3
commit
3acd44f653
@ -96,15 +96,43 @@ public:
|
|||||||
JS Code:
|
JS Code:
|
||||||
```javascript
|
```javascript
|
||||||
var getKthFromEnd = function(head, k) {
|
var getKthFromEnd = function(head, k) {
|
||||||
|
//特殊情况
|
||||||
if(!head) return head;
|
if(!head) return head;
|
||||||
|
//初始化两个指针, 定义指针指向
|
||||||
let pro = head, after = head;
|
let pro = head, after = head;
|
||||||
|
//先移动绿指针到指定位置
|
||||||
for(let i = 0; i < k - 1; i++){
|
for(let i = 0; i < k - 1; i++){
|
||||||
pro = pro.next;
|
pro = pro.next;
|
||||||
}
|
}
|
||||||
|
//两个指针同时移动
|
||||||
while(pro.next){
|
while(pro.next){
|
||||||
pro = pro.next;
|
pro = pro.next;
|
||||||
after = after.next;
|
after = after.next;
|
||||||
}
|
}
|
||||||
|
//返回倒数第k个节点
|
||||||
return after;
|
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
|
||||||
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user