mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 12:14:26 +08:00
规范格式
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user