This commit is contained in:
YDZ
2021-02-21 08:38:32 +08:00
parent 075d7ba884
commit d12dde092f
3 changed files with 27 additions and 0 deletions

View File

@ -20,8 +20,14 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
var fast, slow *ListNode
fast = head
slow = head
step := 0
for i := 0; i < n; i++ {
// n maybe much larger than length of linklist
if fast.Next == nil && step != 0 && step < n-1 {
return head
}
fast = fast.Next
step++
}
if fast == nil {
head = head.Next

View File

@ -29,6 +29,21 @@ func Test_Problem19(t *testing.T) {
qs := []question19{
{
para19{[]int{1, 2}, 2},
ans19{[]int{2}},
},
{
para19{[]int{1}, 1},
ans19{[]int{}},
},
{
para19{[]int{1, 2, 3, 4, 5}, 10},
ans19{[]int{1, 2, 3, 4, 5}},
},
{
para19{[]int{1, 2, 3, 4, 5}, 1},
ans19{[]int{1, 2, 3, 4}},