Reverse a linked list using Golang

Picture of Rezwanul Haque
Rezwanul Haque
Published on
17.09.2025
Time to Read
2 min
Table of Contents

As we start coding and slowly learning about Data Structures(DS), we come across a very famous linear data structure which is known as a linked list. Linked lists and their related questions are quite popular in interviewers who love problem-solving.

What is a Linked List?

A linked list is a common linear data structure. its elements are also known as **Node** are not stored at a contiguous location. The nodes are linked using pointers. Linked lists are popular as their size is not fixed like an array. Linked list’s each node contains a value and a pointer to the next node. The head pointer points to the first node, and the last node of the list points to null. When the linked list is empty, the head pointer points to null.

Types of Linked Lists:
1. Singly Linked List (Uni-directional)
2. Doubly Linked List (Bi-directional)
3. Circular Linked List

I’ll write about linked list types in a different post as this post about reversing a linked list.

Pros:

  1. Dynamically increase in size
  2. Easy to insertion/deletion

Cons:

  1. Accessing a random node is not allowed.
  2. Additional memory space needs for each node in a linked list.
  3. Not cache friendly.

Linked List

Node for a linked list

				
					type Node struct {
    prev *Node
    next *Node
    key interface{}
}

type LinkedList struct {
    head *Node
    tail *Node
}
				
			

Push method for a Linked List

				
					func (ll *LinkedList) Push(key interface{}) {
    list := &Node{
    next: ll.head,
    key: key,
    }
    if ll.head != nil {
        ll.head.prev = list
    }
    ll.head = list
    
    l := ll.head
    for ll.next != nil {
        l = l.next
    }
    ll.tail = l
}
				
			

Display a Linked list

				
					func (ll *LinkedList) Display() {
    list := ll.head
    for list != nil {
        fmt.Printf("%+v ->", list.key)
        list = list.next
    }
    fmt.Println()
}

// normal display function
func Display(list *Node) {
    for list != nil {
        fmt.Printf("%v ->", list.key)
        list = list.next
    }
    fmt.Println()
}
				
			

Reverse a Linked list

				
					func (ll *LinkedList) Reverse() {
    currentNode := ll.head
    var next *Node
    var previousNode *Node
    ll.tail = ll.head

    for currentNode != nil {
        next, currentNode.next = currentNode.next, previousNode
        previousNode, currentNode = currentNode, next
    }
    ll.head = previousNode
    Display(ll.head)
}
				
			

Main function

				
					func main() {
    link := LinkedList{}
    link.Push(9)
    link.Push(12)
    link.Push(15)
    link.Push(8)
    link.Push(1)
    link.Push(3)

    fmt.Println("==============================")
    fmt.Printf("Head: %v\n", link.head.key)
    fmt.Printf("Tail: %v\n", link.tail.key)
    link.Display()
    fmt.Println("==============================")
    link.Reverse()
    fmt.Printf("head: %v\n", link.head.key)
    fmt.Printf("tail: %v\n", link.tail.key)
    fmt.Println("==============================")
}

// output
==============================
Head: 3
Tail: 9
3 -> 1 -> 8 -> 15 -> 12 -> 9 -> 
==============================
9 -> 12 -> 15 -> 8 -> 1 -> 3 -> 
head: 9
tail: 3
==============================
				
			
50+ companies rely on our top 1% talent to scale their dev teams.
Excellence Our minimum bar.
It has become a prerequisite for companies to develop custom software.
We've stopped counting. Over 50 brands count on us.
Our company specializes in software outsourcing and provides robust, scalable, and efficient solutions to clients around the world.
klikit

Chris Withers

CEO & Founder, Klikit

Klikit-logo
Heartfelt appreciation to Vivasoft Limited for believing in my vision. Their talented developers can take any challenges against all odds and helped to bring Klikit into life.appreciation to Vivasoft Limited for believing in my vision. Their talented developers can take any challenges.
Start with a dedicated squad in 7 days

NDA first, transparent rates, agile delivery from day one.

Where We Build the Future
Scale Engineering Without the Overhead

Elastic offshore teams that integrate with your processes and timezone.

Tech Stack
0 +
Blogs You May Love

Don’t let understaffing hold you back. Maximize your team’s performance and reach your business goals with the best IT Staff Augmentation

let's build our future together

Get to Know Us Better

Explore our expertise, projects, and vision.