首页 > 其他 > 详细

翻转链表

时间:2020-09-06 14:28:03      阅读:53      评论:0      收藏:0      [点我收藏+]

反转一个单链表。

示例:

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


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* Pre=NULL, *Cur=head;
        while(Cur){
            ListNode* Next = Cur->next;
            Cur->next = Pre;
            Pre = Cur;
            Cur = Next;
        }
        return Pre;
    }
};

 

翻转链表

原文:https://www.cnblogs.com/wsw-seu/p/13621163.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!