206. 反转链表

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

public ListNode reverseList(ListNode L){
        ListNode h = null;//前驱
        ListNode t = null;//后继
        ListNode cur = L;//当前
        //以链表:1->2->3->4->5为例:
        //第一步:cur=1,t=2,h=null(结束时要保证cur.next=null)
        //第二步:h=1(cur),cur=2(t)
        //...重复上述步骤
        while(cur != null){
            t = cur.next;
            cur.next = h;
            h = cur;
            cur = t;
        }
        return h;
    }

 

posted @ 2020-10-20 10:06  进击的小渣渣!!  阅读(68)  评论(0)    收藏  举报