惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
Latest news
Latest news
Help Net Security
Help Net Security
S
Security Affairs
Webroot Blog
Webroot Blog
The Hacker News
The Hacker News
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tor Project blog
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Help Net Security
L
Lohrmann on Cybersecurity
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
MongoDB | Blog
MongoDB | Blog
Cyberwarzone
Cyberwarzone
The Last Watchdog
The Last Watchdog
S
Securelist
N
News and Events Feed by Topic
S
Secure Thoughts
F
Fortinet All Blogs
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
M
MIT News - Artificial intelligence
F
Full Disclosure
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
P
Privacy International News Feed
L
LangChain Blog
Know Your Adversary
Know Your Adversary
C
CERT Recently Published Vulnerability Notes

博客园 - 茁壮的小草

join查询中索引建议 tomcat连接池的“bug” 连接池介绍 JDBC介绍 Hadoop完全分布式搭建 Apache Hadoop YARN 使用 kubebuilder 创建并部署 k8s-operator 单链表操作 如何在Kubernetes 里添加自定义的 API 对象(一) 搭建golang开发环境(1.14之后版本) 【源码】Redis命令处理过程 K8S配置存活、就绪和启动探测器 【源码】Redis exists命令bug分析 【源码】Redis Server启动过程 K8S中的Objects MAC 安装MySQL-python 公司里使用gitlab管理项目 vscode配置django开发环境最佳实战(mac) virtualenv最佳实战(windows)
单链表反转
茁壮的小草 · 2022-03-04 · via 博客园 - 茁壮的小草

/* 
题目: 给你单链表的头节点 head ,请你反转链表,并返回反转后的链表
*/
#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode {
     int val;
     struct ListNode *next;
}ListNode;

//初始化一个单链表
ListNode * link_list_create_from_tail(int *data,int length)
{
    ListNode *head = (ListNode *)malloc(sizeof(ListNode));
    if(head == NULL){
        printf("申请空间失败");
        return NULL;
    }
    //初始化链表为空
    head->next = NULL;

    //必须申明一个指向新插入结点的指针,用来标识插入新结点位置
    //开始时候p指向head结点
    ListNode *p = head;

    for(int i=0;i<length;i++){
        ListNode *node = (ListNode *)malloc(sizeof(ListNode));
        node->val = data[i];
        //由于每次插入都是最后一个,所以结点的next指针始终指向NULL
        node->next = NULL;
        //前一个结点指针就是p的next指向新结点
        p->next = node;

        //然后把指针指向新结点
        p = node;
    }
    return head->next;
}

// 反转单链表
struct ListNode* reverseList(struct ListNode* head){
    //前驱指针
    struct ListNode *prev = NULL;
    //遍历用的当前指针
    struct ListNode *curr = head;
    while(curr){
        //每次创建新的next指针,指向下一个已经断开的结点
        struct ListNode *next = curr->next;
        //当前指针反向指向前驱结点
        curr->next = prev;
        //后移前驱结点到当前指针位置
        prev = curr;
        //后移当前指针到下一个结点
        curr = next;
    }
    return prev;
}

//遍历单向链表
//head: 传入头结点
void print_linkedlist(ListNode * head)
{   
    if(head == NULL)
    {
        printf("单链表为空");
    }
    //由于头结点不存储数据,故从下一个结点遍历数据
    //申明指针p,指向头结点的下一结点
    ListNode *p = head;
    //循环链表,直到指针p为空
    while(p)
    {
        printf("%d ",p->val);
        //指针p循环指向下一结点
        p = p->next;
    }
}

int main(int argc,char *argv[])
{
    int data1[2] = {1,6};
    ListNode *tail1 = link_list_create_from_tail(data1,2);
    print_linkedlist(tail1);
    printf("\n");
   
    ListNode *head = reverseList(tail1);
    print_linkedlist(head);

    
    return 0;
}