-
Notifications
You must be signed in to change notification settings - Fork 7
/
interview_02.02_test.go
65 lines (54 loc) · 1.16 KB
/
interview_02.02_test.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//实现一种算法,找出单向链表中倒数第 k 个节点。返回该节点的值。
//
// 注意:本题相对原题稍作改动
//
// 示例:
//
// 输入: 1->2->3->4->5 和 k = 2
//输出: 4
//
// 说明:
//
// 给定的 k 保证是有效的。
// Related Topics 链表 双指针
// 👍 70 👎 0
package leetcode
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestKthToLast(t *testing.T) {
assert.Equal(t, 4, kthToLast(NewList([]int{1, 2, 3, 4, 5}), 2))
assert.Equal(t, 3, kthToLast(NewList([]int{1, 2, 3, 4, 5}), 3))
assert.Equal(t, 1, kthToLast(NewList([]int{1, 2, 3, 4, 5}), 5))
assert.Equal(t, 5, kthToLast(NewList([]int{1, 2, 3, 4, 5}), 1))
}
func kthToLast(head *ListNode, k int) int {
if head == nil {
return 0
}
var (
cursor = head
firstCursor = head
secondCursor *ListNode
count = 0
)
for cursor != nil {
count += 1
if count == k {
secondCursor = cursor
break
}
cursor = cursor.Next
}
if secondCursor == nil {
return 0
}
firstCursor = head
cursor = secondCursor
for cursor.Next != nil {
cursor = cursor.Next
firstCursor = firstCursor.Next
}
return firstCursor.Val
}