Given a linked list, remove the n-th node from the end of list and return its head.
Example:
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Follow up:
Could you do this in one pass?
Tags: Linked List, Two Pointers
题意是让你删除链表中的倒数第 n 个数
我的解法是利用双指针,这两个指针相差 n 个元素,当后面的指针扫到链表末尾的时候,自然它前面的那个指针所指向的下一个元素就是要删除的元素,即 pre.next = pre.next.next;,但是如果一开始后面的指针指向的为空,此时代表的意思就是要删除第一个元素,即 head = head.next;。
func removeNthFromEnd(head *ListNode, n int) *ListNode {
fastNode := head
slowNode := head
for n > 0 {
fastNode = fastNode.Next
n--
}
if fastNode != nil {
for fastNode.Next != nil {
fastNode = fastNode.Next
slowNode = slowNode.Next
}
slowNode.Next = slowNode.Next.Next
} else {
head = head.Next
}
return head
}
思路2 ```go
```
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:awesome-golang-algorithm