反转链表(C语言)

题目:输入一个链表头,反转链表并返回反转后的链表头

 


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* reverseList(struct ListNode* head){
    if (head == NULL)
        return NULL;
    struct ListNode *pt_pre;
    struct ListNode *pt_cur;
    struct ListNode *pt_next;

    if (head == NULL)
        return NULL;
    pt_pre = head;
    if (head->next == NULL)
        return head;
    pt_cur     = pt_pre->next;
    head->next = NULL;

    while (pt_cur != NULL){
        pt_next      = pt_cur->next;
        pt_cur->next = pt_pre;
        pt_pre       = pt_cur;
        pt_cur       = pt_next;
    }
    return pt_pre;
}
 

 

posted @ 2020-06-08 18:54  LvCS  阅读(420)  评论(0)    收藏  举报