Problem List

Reverse Linked List

May 24, 2025Go linked list

This was one of the methods on a project in DSA so implementation was fairly straightforward. The basic methodology is that you must keep track of three running variables at a time. There's previous node, current node, and next node. The previous node always starts off as null because it will be the tail's previous at the end. The current node initializes as the head node, and next is current's next. Then you essentially just point current to previous, then increment all of the pointers.

Ex:

1 -> 2 -> 3

null <- 1 2 -> 3

Now swap curr to point to the previous. (1->null), and then update the rest of the values

null <- 1 <- 2 3

Repeat this process & the entire Linked List will end up reversed.

1 <- 2 <- 3

or

3 -> 2 -> 1

Go Solution
func reverseList(head *ListNode) *ListNode {
	var prev *ListNode 
	curr := head

	for curr != nil {
        next := curr.Next
		curr.Next = prev
		prev = curr
		curr = next
	}

	return prev
}
LeetCode Problem Link