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

链表-Reverse Linked List

时间:2016-03-08 14:55:28      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseList(struct ListNode* head) {
    if(!head||!head->next)
        return head;
    struct ListNode *cur=head;
    struct ListNode *tail=head;
    while(tail->next)
        tail=tail->next;
    while(cur!=tail)
    {
        head=cur->next;
        cur->next=tail->next;
        tail->next=cur;
        cur=head;
    }
    return head;
}

有一个问题,leetcode给出的链表测试数据应该是不包含头结点,所以写代码的时候需要注意一下。

 

链表-Reverse Linked List

标签:

原文地址:http://www.cnblogs.com/summerkiki/p/5254044.html

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