leet-code/middle-of-the-linked-list/sol.go

18 lines
237 B
Go
Raw Normal View History

2021-11-09 15:56:54 +00:00
package main
type ListNode struct {
Val int
Next *ListNode
}
func middleNode(head *ListNode) *ListNode {
mid, end := head, head
for c := 0; end != nil; c++ {
end = end.Next
if c%2 == 1 {
mid = mid.Next
}
}
return mid
}