remove-nth-node-from-end-of-list

This commit is contained in:
Gleb Koval 2021-11-09 16:10:49 +00:00
parent a33c19f2cc
commit d6844d9388
No known key found for this signature in database
GPG Key ID: DF27F6A77C48FDA0
1 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package main
type ListNode struct {
Val int
Next *ListNode
}
func removeNthFromEnd(head *ListNode, n int) *ListNode {
top := &ListNode{Val: 0, Next: head}
parent := top
for current, c := head, 1; current != nil; current, c = current.Next, c+1 {
if c > n {
parent = parent.Next
}
}
parent.Next = parent.Next.Next
return top.Next
}