16-1 单链表的简单翻转

一、题目描述

对链表整体做翻转

二、解题思路

定义三个指针*pre 、*cur 、*next

三、解题算法

/*****************************************************
Author:tmw
Date:2018-4-13
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct ListNode
{
    int val;
    struct ListNode *next;
}ListNode;

/**简单版:单链表的整体reverse**/
ListNode* reverseList(struct ListNode* head) {

    if( head == NULL )
        return NULL;

    ListNode* pre;
    ListNode* cur;
    ListNode* next;

    pre=NULL;
    cur=head;

    /**当链表只有一个元素:即头结点时**/
    if(head->next==NULL)
        return head;

    while( cur->next != NULL )
    {
        next = cur->next;
        cur->next = pre;
        pre = cur;
        cur = next;
    }
    /**将最后的逆转补上**/
    cur->next = pre;
    return cur;

}


梦想还是要有的,万一实现了呢~~~~ヾ(◍°∇°◍)ノ゙~~~

猜你喜欢

转载自blog.csdn.net/qiki_tangmingwei/article/details/80935710
今日推荐