-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy path19.go
45 lines (42 loc) · 857 Bytes
/
19.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
package easy_collection
/**
* 19. Remove Nth Node From End of List
* <p>
* Given a linked list, remove the n-th node from the end of list and return its head.
* <p>
* Example:
* <p>
* Given linked list: 1->2->3->4->5, and n = 2.
* <p>
* After removing the second node from the end, the linked list becomes 1->2->3->5.
* <p>
* Note:
* <p>
* Given n will always be valid.
* <p>
* Follow up:
* <p>
* Could you do this in one pass?
*/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeNthFromEnd(head *ListNode, n int) *ListNode {
start := &ListNode{Val: 0}
slow := start
fast := start
slow.Next = head
for i := 0; i <= n; i++ {
fast = fast.Next
}
for fast != nil {
slow = slow.Next
fast = fast.Next
}
slow.Next = slow.Next.Next
return start.Next
}