0019. Remove Nth Node From End of List

# 19. Remove Nth Node From End of List#

## 题目 #

Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.

Follow up: Could you do this in one pass?

Example 1:

``````Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]

``````

Example 2:

``````Input: head = [1], n = 1
Output: []

``````

Example 3:

``````Input: head = [1,2], n = 1
Output: [1]

``````

Constraints:

• The number of nodes in the list is `sz`.
• `1 <= sz <= 30`
• `0 <= Node.val <= 100`
• `1 <= n <= sz`

## 代码 #

``````package leetcode

import (
"github.com/halfrost/leetcode-go/structures"
)

// ListNode define
type ListNode = structures.ListNode

/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/

// 解法一
func removeNthFromEnd(head *ListNode, n int) *ListNode {
for fast != nil {
if n <= 0 {
preSlow = slow
slow = slow.Next
}
n--
fast = fast.Next
}
preSlow.Next = slow.Next
}

// 解法二
func removeNthFromEnd1(head *ListNode, n int) *ListNode {
return nil
}
if n <= 0 {
}
len := 0
for current != nil {
len++
current = current.Next
}
if n > len {
}
if n == len {
current.Next = nil
}
i := 0
for current != nil {
if i == len-n-1 {
deleteNode := current.Next
current.Next = current.Next.Next
deleteNode.Next = nil
break
}
i++
current = current.Next
}