码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode 简单】 第六十题 反转链表

时间:2018-08-23 02:15:54      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:linked   方法   简单   str   ever   nbsp   输入   bsp   for   

反转一个单链表。

示例:

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

进阶:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseList(struct ListNode* head) {
    if (NULL == head || NULL == head->next)
    {
        return head;
    }
    struct ListNode* a=head;
    struct ListNode* b=head->next;
    struct ListNode* c;
    
    head->next = NULL;
    while (b)
    {
        c = b->next;
        b->next = a;
        a = b;
        b = c;
    }
    head = a;
    return head;
}

 

【leetcode 简单】 第六十题 反转链表

标签:linked   方法   简单   str   ever   nbsp   输入   bsp   for   

原文地址:https://www.cnblogs.com/flashBoxer/p/9521336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!