mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-23 09:51:45 +08:00
39 lines
581 B
Go
39 lines
581 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// ListNode define
|
|
type ListNode = structures.ListNode
|
|
|
|
/**
|
|
* Definition for singly-linked list.
|
|
* type ListNode struct {
|
|
* Val int
|
|
* Next *ListNode
|
|
* }
|
|
*/
|
|
|
|
func middleNode(head *ListNode) *ListNode {
|
|
if head == nil || head.Next == nil {
|
|
return head
|
|
}
|
|
p1 := head
|
|
p2 := head
|
|
for p2.Next != nil && p2.Next.Next != nil {
|
|
p1 = p1.Next
|
|
p2 = p2.Next.Next
|
|
}
|
|
length := 0
|
|
cur := head
|
|
for cur != nil {
|
|
length++
|
|
cur = cur.Next
|
|
}
|
|
if length%2 == 0 {
|
|
return p1.Next
|
|
}
|
|
return p1
|
|
}
|