好久不写链表操作了,感觉自己像个傻子

好久不写链表操作了,整理了半天指针才搞清楚,感觉脑子退化了。。

链表翻转操作

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func reverseList(head *ListNode) *ListNode {
    var pre *ListNode
    cur := head
    for{
        if cur == nil {
            break;
        }
        next := cur.Next
        cur.Next = pre
        pre = cur
        cur = next
    }
    return pre
}

leetcode : https://leetcode-cn.com/submissions/detail/168031969/

发表新评论