力扣206题(C语言)反转链表

反转一个单链表

力扣206题(C语言)反转链表_第1张图片

代码:

/**
 * 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;
}

你可能感兴趣的:(#反转链表)