Reverse Linked List

Solution in Go

func reverseList(head *ListNode) *ListNode {
    if head == nil || head.Next == nil {
        return head
    }

    current := head
    var previous *ListNode

    for current != nil {
        next := current.Next
        current.Next = previous
        previous = current
        current = next
    }

    return previous
}
Subscribe via email

Get notified once/twice per month when new articles are published.

Copyright © 2022 - 2023 TheDeveloperCafe.
The Go gopher was designed by Renee French.