反转链表(一)

阅读 55

2022-04-14

balabala:最近学习在学习数据结构刚好学好链表

1.题目:https://leetcode-cn.com/problems/reverse-linked-list/

2.

struct ListNode* reverseList(struct ListNode* head){
    struct ListNode *curr=head;
    struct ListNode *pre=NULL;
    struct ListNode *next=NULL;
    while(curr)
    {
        next=curr->next;
        curr->next = pre;
        pre=curr;
        curr=next;
    }

    return pre;
}

复杂度分析

  •     时间复杂度:O(n),其中 n 是链表的长度。需要遍历链表一次。
  •     空间复杂度:O(1)

精彩评论(0)

0 0 举报