反转链表

题目:

反转一个单链表。

示例:

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

class Solution {
    public ListNode reverseList(ListNode head) {
        
        ListNode pre=null;
        ListNode cur=head;
        
        while(cur!=null)
        {
            ListNode temp=cur.next;
            cur.next=pre;
            pre=cur;
            cur=temp;
        }
        
        return pre;
    }
}

 

posted @ 2019-05-16 19:19  彩色的梦  阅读(126)  评论(0编辑  收藏  举报