0
点赞
收藏
分享

微信扫一扫

反转链表(一)

云朵里的佛光 2022-04-14 阅读 54
c#

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 条评论