From a2bebb36dfb7058bcd478aa8aa4e72e9fe5e3558 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 3 Mar 2024 20:51:00 +0100 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB19.=20Remove=20Nth=20Node=20F?= =?UTF-8?q?rom=20End=20of=20List=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/remove-nth-node-from-end-of-list.go | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 go/remove-nth-node-from-end-of-list.go diff --git a/go/remove-nth-node-from-end-of-list.go b/go/remove-nth-node-from-end-of-list.go new file mode 100644 index 0000000..6288f88 --- /dev/null +++ b/go/remove-nth-node-from-end-of-list.go @@ -0,0 +1,29 @@ +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 +}