1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/go/remove-nth-node-from-end-of-list.go

30 lines
461 B
Go
Raw Normal View History

package remove_nth_node_from_end_of_list
/**
* 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
}