JZ15 反转链表

题目描述

输入一个链表,反转链表后,输出新链表的表头。
 
func ReverseList( head *ListNode ) *ListNode {
    // write code here
    if head == nil || head.Next == nil {
        return head
    }

    var pre, cur, post *ListNode = nil, head, head.Next
    for cur != nil {
        post = cur.Next
        cur.Next = pre
        pre = cur
        cur = post 
    }
    return pre
}

 

posted @ 2021-04-08 00:14  zqlucky  阅读(50)  评论(0编辑  收藏  举报