Different: Golang O(n) space, like reversing a string


  • 0
    A
    func reverseList(head *ListNode) *ListNode {
        var a []*ListNode
        for n := head; n != nil; n = n.Next {
            a = append(a, n)    
        }
        for i, j := 0, len(a)-1; i < j; i, j = i + 1, j - 1 {
            a[i].Val, a[j].Val = a[j].Val, a[i].Val
        }
        return head
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.