[LeetCode By Go 50]206. Reverse Linked List

题目

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
}
    原文作者:miltonsun
    原文地址: https://www.jianshu.com/p/9f75ef746fef
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞