Dashboard Temp Share Shortlinks Frames API

HTMLify

LeetCode - Middle of the Linked List - Go
Views: 334 | Author: abh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func middleNode(head *ListNode) *ListNode {
	var elements []int
	t_head := head
	for ;t_head!=nil; {
		elements = append(elements, t_head.Val)
		t_head = t_head.Next
	}
	mid := len(elements)/2
	for range mid {
		head = head.Next
	}
	return head
}