力扣206題(C語言)反轉鏈表

反轉一個單鏈表

在這裏插入圖片描述

代碼:

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

typedef struct ListNode Node;
struct ListNode* reverseList(struct ListNode* head){
   Node* cur = head;
   Node* newhead = NULL;


    while(cur)
    {
       Node* next = cur->next;
       cur->next = newhead;
       newhead = cur;
       cur = next;
    }
    return newhead;
}
發佈了25 篇原創文章 · 獲贊 8 · 訪問量 2181
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章