1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/go/remove-nth-node-from-end-of-list.go
Matej Focko d2ef757754
go: allow testing
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-06-16 11:44:08 +02:00

29 lines
433 B
Go

package main
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeNthFromEnd(lst *ListNode, n int) *ListNode {
toRemove := lst
head := lst
for i := 0; i < n; i++ {
head = head.Next
}
if head == nil {
return toRemove.Next
}
for head.Next != nil {
toRemove = toRemove.Next
head = head.Next
}
toRemove.Next = toRemove.Next.Next
return lst
}