- LeetCode 19. Remove Nth Node Fro
- 19. Remove Nth Node From End of
- 19. 删除倒数第N个结点
- LeetCode 19. Remove Nth Node Fro
- leetcode #19 Remove Nth Node Fro
- LeetCode-19 - Remove Nth Node Fr
- Remove Nth Node From End of List
- 19. Remove Nth Node From End of
- 19. Remove Nth Node From End of
- Remove Nth Node From End of List

解决思路
注意:题目要求一次遍历完成。
定义两个指针,fast先走n步,fast走到结尾,slow可以定位到要删除节点,可完成删除。
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeNthFromEnd(head *ListNode, n int) *ListNode {
if head == nil {
return nil
}
var slow *ListNode
var fast *ListNode = head
for {
if fast.Next == nil {
if slow == nil {
return head.Next
}else {
slow.Next = slow.Next.Next
return head
}
}
n --
if n == 0 {
slow = head
}
if n < 0 {
slow = slow.Next
}
fast = fast.Next
}
}
网友评论