0019. Remove Nth Node From End of List

# 19. Remove Nth Node From End of List#

## 题目 #

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.

``````

## 代码 #

``````
package leetcode

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

// 解法一
func removeNthFromEnd(head *ListNode, n int) *ListNode {
var fast, slow *ListNode
for i := 0; i < n; i++ {
fast = fast.Next
}
if fast == nil {
}
for fast.Next != nil {
fast = fast.Next
slow = slow.Next
}
slow.Next = slow.Next.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
}