当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 【leetcode 简单】 第六十题 反转链表

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

2018年08月23日  | 移动技术网IT编程  | 我要评论

反转一个单链表。

示例:

输入: 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;
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网