题目描述
实现一种算法,找出单向链表中倒数第 k 个节点。返回该节点的值。
注意: 本题相对原题稍作改动
示例:
输入: 1->2->3->4->5 和 k = 2
输出: 4
说明:
给定的 k 保证是有效的。
解法
方法一:快慢指针
我们定义两个指针 slow
和 fast
,初始时都指向链表头节点 head
。然后 fast
指针先向前移动 \(k\) 步,然后 slow
和 fast
指针同时向前移动,直到 fast
指针指向链表末尾。此时 slow
指针指向的节点就是倒数第 \(k\) 个节点。
时间复杂度 \(O(n)\) ,其中 \(n\) 是链表的长度。空间复杂度 \(O(1)\) 。
Python3 Java C++ Go TypeScript Rust JavaScript Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 # Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution :
def kthToLast ( self , head : ListNode , k : int ) -> int :
slow = fast = head
for _ in range ( k ):
fast = fast . next
while fast :
slow = slow . next
fast = fast . next
return slow . val
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int kthToLast ( ListNode head , int k ) {
ListNode slow = head , fast = head ;
while ( k -- > 0 ) {
fast = fast . next ;
}
while ( fast != null ) {
slow = slow . next ;
fast = fast . next ;
}
return slow . val ;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public :
int kthToLast ( ListNode * head , int k ) {
ListNode * fast = head ;
ListNode * slow = head ;
while ( k -- ) {
fast = fast -> next ;
}
while ( fast ) {
slow = slow -> next ;
fast = fast -> next ;
}
return slow -> val ;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func kthToLast ( head * ListNode , k int ) int {
slow , fast := head , head
for ; k > 0 ; k -- {
fast = fast . Next
}
for fast != nil {
slow = slow . Next
fast = fast . Next
}
return slow . Val
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 /**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function kthToLast ( head : ListNode | null , k : number ) : number {
let [ slow , fast ] = [ head , head ];
while ( k -- ) {
fast = fast . next ;
}
while ( fast !== null ) {
slow = slow . next ;
fast = fast . next ;
}
return slow . val ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 // Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn kth_to_last ( head : Option < Box < ListNode >> , k : i32 ) -> i32 {
let mut fast = & head ;
for _ in 0 .. k {
fast = & fast . as_ref (). unwrap (). next ;
}
let mut slow = & head ;
while let ( Some ( f ), Some ( s )) = ( fast , slow ) {
fast = & f . next ;
slow = & s . next ;
}
slow . as_ref (). unwrap (). val
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 /**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {number}
*/
var kthToLast = function ( head , k ) {
let [ slow , fast ] = [ head , head ];
while ( k -- ) {
fast = fast . next ;
}
while ( fast !== null ) {
slow = slow . next ;
fast = fast . next ;
}
return slow . val ;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 /**
* Definition for singly-linked list.
* public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ x: Int, _ next: ListNode? = nil) {
* self.val = x
* self.next = next
* }
* }
*/
class Solution {
func kthToLast ( _ head : ListNode ?, _ k : Int ) -> Int {
var slow = head
var fast = head
var k = k
while k > 0 {
fast = fast ?. next
k -= 1
}
while fast != nil {
slow = slow ?. next
fast = fast ?. next
}
return slow ?. val ?? 0
}
}