《leetCode》:Reverse Linked List

题目

Reverse a singly linked list.

思路

这个题也相当简单。借用接个指针来完成即可。

/**
 * 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* cur=head;
    struct ListNode* pre=NULL;
    struct ListNode* next=NULL;
    while(cur!=NULL){
        next=cur->next;
        cur->next=pre;
        pre=cur;
        cur=next;
    }
    return pre;
}

你可能感兴趣的:(LeetCode,struct,指针,LinkedList,reverse)