首页 > 其他 > 详细

leetcode-206-Reverse Linked List

时间:2020-07-16 18:03:04      阅读:49      评论:0      收藏:0      [点我收藏+]

思路:

1->2->3->4->5   要实现翻转

1、让a=head,b=head->next,

2、翻转

auto c=b->next

b->next=a

a=b

b=c

3.直到head->next=null

4.返回a

 

代码:

/**
 * 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* cur = NULL,*pre=head;
        // while(pre !=NULL)
        // {
        //     ListNode* t=pre->next;
        //     pre->next=cur;
        //     cur=pre;
        //     pre=t;
        // }
        // return cur;
    if(!head) return NULL;

    auto  a=head,b=head->next;
    while(b)
    {
        auto c=b->next;
        b->next=a;
        a=b;
        b=c;
    }
    head->next=NULL;

    return a;
    }
};

leetcode-206-Reverse Linked List

原文:https://www.cnblogs.com/Sunshineboy1/p/13323376.html

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