From f65d94b76957c7cb63775a0aef39b172099ac720 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 7 Mar 2024 23:00:05 +0100 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB876.=20Middle=20of=20the=20Li?= =?UTF-8?q?nked=20List=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/middle-of-the-linked-list.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 go/middle-of-the-linked-list.go diff --git a/go/middle-of-the-linked-list.go b/go/middle-of-the-linked-list.go new file mode 100644 index 0000000..025ab5d --- /dev/null +++ b/go/middle-of-the-linked-list.go @@ -0,0 +1,20 @@ +package middle_of_the_linked_list + +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func middleNode(head *ListNode) *ListNode { + x := head + y := head + + for y != nil && y.Next != nil { + x = x.Next + y = y.Next.Next + } + + return x +}