美文网首页LeetCode By Go
[LeetCode By Go 50]206. Reverse

[LeetCode By Go 50]206. Reverse

作者: miltonsun | 来源:发表于2017-08-22 18:27 被阅读7次

题目

Reverse a singly linked list.

解题思路

头插法

代码

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func reverseList(head *ListNode) *ListNode {
    if nil == head {
        return nil
    }

    ret := head
    head = head.Next
    ret.Next = nil

    for ; head != nil; {
        tmp := head
        head = head.Next
        tmp.Next = ret

        ret = tmp
    }
    
    return ret
}

相关文章

网友评论

    本文标题:[LeetCode By Go 50]206. Reverse

    本文链接:https://www.haomeiwen.com/subject/kvnldxtx.html