zoukankan      html  css  js  c++  java
  • leetcode206题实现反转链表(c语言)

     

    迭代方法实现:

    struct ListNode* reverseList(struct ListNode* head){
        struct ListNode *temp , *new_head=NULL;
        while(head)
        {
           temp=head;
           head = head->next;
           temp->next = new_head;
           new_head = temp;
        }
        return new_head;
    }

    递归方法实现:

    struct ListNode* reverseList(struct ListNode* head){
        if (head == NULL || head->next == NULL)
            return head;
        else
        {
            struct ListNode *newhead = reverseList(head->next);
            head->next->next = head;
            head->next = NULL;
            return newhead;
            
        }
    }

    已经忘记了迭代和递归的关系,特意去复习了一下:

    递归是由自己延伸出去的,而迭代是得到新的结果并替代了自己 。

    1.递归是指函数过程,子程序在运行过程中直接或间接调用自身而产生的重入现象。即函数不断引用自身,直到引用的对象已知

    2.迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果,每一次对过程重复一次称为一次"迭代“,而每一次迭代得到的结果会作为下一次迭代的初始值。

  • 相关阅读:
    sql排序对比(row_number,rank,dense_rank)
    SQL分组排名+行转列
    MS SQL 权限设置脚本
    centos8容器中安装lamp及wordpress
    MacOS禁止向日葵开机启动
    docker(1)
    centos7的firewalld
    ssh免密码
    CENTOS7安装vsftp
    centos 7 安装samba配置匿名共享文件夹
  • 原文地址:https://www.cnblogs.com/redzzy/p/13138172.html
Copyright © 2011-2022 走看看