// Node is a node of a linked list with an integer value.
type Node[T comparable] struct {
// LinkedList is a linked list of nodes, with a head node.
type LinkedList[T comparable] struct {
// Add adds a node to the end of the linked list.
func (l *LinkedList[T]) Add(value T) {
node := &Node[T]{value: value}
// Remove removes a node from the linked list.
func (l *LinkedList[T]) Remove(value T) error {
return fmt.Errorf("empty list")
if l.head.value == value {
if prev.next.value == value {
prev.next = prev.next.next
return fmt.Errorf("value %v not found", value)
// Size returns the size of the linked list.
func (l *LinkedList[T]) Size() int {
// IsEmpty returns true if the linked list is empty.
func (l *LinkedList[T]) IsEmpty() bool {
// Contains returns true if the linked list contains the value.
func (l *LinkedList[T]) Contains(value T) bool {
// String regresa un string que representa la lista enlazada.
func (l *LinkedList[T]) String() string {
s += fmt.Sprintf("%v", node.value)
func New[T comparable]() *LinkedList[T] {
return &LinkedList[T]{size: 0}
fmt.Println("Elementos:", ll)
fmt.Println("Tamaño:", ll.Size())
fmt.Println("¿Está vacía?", ll.IsEmpty())
fmt.Println("¿Contiene el número 2?", ll.Contains(2))
fmt.Println("¿Contiene el número 4?", ll.Contains(4))
fmt.Printf("\nℹ️ Elemento número 2 eliminado...\n\n")
fmt.Println("Elementos:", ll)
fmt.Println("Tamaño:", ll.Size())
fmt.Println("¿Está vacía?", ll.IsEmpty())
fmt.Printf("\nℹ️ Lista enlazada de tipo string creada...\n\n")
fmt.Println("Elementos:", ll2)
fmt.Println("Tamaño:", ll2.Size())
fmt.Println("¿Contiene la letra 'a'?", ll2.Contains("a"))