code:
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeNthFromEnd(head *ListNode, n int) *ListNode {
i, listRet, temp1 := 0, head, head
for ; i < n && temp1 != nil; i++ {
temp1 = temp1.Next
}
if i < n {
listRet = nil
} else if nil == temp1 {
listRet = head.Next
} else {
preNthNode := head
for temp1.Next != nil {
preNthNode, temp1 = preNthNode.Next, temp1.Next
}
preNthNode.Next = preNthNode.Next.Next
}//if i < n else
return listRet
}
result:
personal opinion:
easy, one pass