midpoint

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2026 License: BSD-3-Clause Imports: 0 Imported by: 0

README

NAME

19 - find midpoint in a singly linked list

DESCRIPTION

Problem

Given a singly linked list, find midpoint element. If the number of items is even, select the second out of two elements in the middle.

Example

Input: 1, 2, 3, 4

Output: 3

SEE ALSO

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	Val  int
	Next *Node
}

func Find

func Find(node *Node) *Node
Example (EvenNumberOfItems)
package main

import (
	"fmt"

	"github.com/skhal/lab/iq/19/midpoint"
)

func main() {
	list := &midpoint.Node{
		Val: 1,
		Next: &midpoint.Node{
			Val: 2,
			Next: &midpoint.Node{
				Val: 3,
				Next: &midpoint.Node{
					Val: 4,
				},
			},
		},
	}
	node := midpoint.Find(list)
	fmt.Println(node.Val)
}
Output:
3
Example (OddNumberOfItems)
package main

import (
	"fmt"

	"github.com/skhal/lab/iq/19/midpoint"
)

func main() {
	list := &midpoint.Node{
		Val: 1,
		Next: &midpoint.Node{
			Val: 2,
			Next: &midpoint.Node{
				Val: 3,
			},
		},
	}
	node := midpoint.Find(list)
	fmt.Println(node.Val)
}
Output:
2

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL