ZT 链表逆序
设链表节点为
- typedef struct tagListNode{
- int data;
- struct tagListNode* next;
- }ListNode, *List;
要求将一带链表头List head的单向链表逆序。
分析:
1). 若链表为空或只有一个元素,则直接返回;
2). 设置两个前后相邻的指针p,q. 将p所指向的节点作为q指向节点的后继;
3). 重复2),直到q为空
4). 调整链表头和链表尾
示例:以逆序A->B->C->D为例,图示如下

实现及测试代码如下:
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct tagListNode{
- int data;
- struct tagListNode* next;
- }ListNode, *List;
- void PrintList(List head);
- List ReverseList(List head);
- int main()
- {
- //分配链表头结点
- ListNode *head;
- head = (ListNode*)malloc(sizeof(ListNode));
- head->next = NULL;
- head->data = -1;
- //将[1,10]加入链表
- int i;
- ListNode *p, *q;
- p = head;
- for(int i = 1; i <= 10; i++)
- {
- q = (ListNode *)malloc(sizeof(ListNode));
- q->data = i;
- q->next = NULL;
- p->next = q;
- p = q;
- }
- PrintList(head); /*输出原始链表*/
- head = ReverseList(head); /*逆序链表*/
- PrintList(head); /*输出逆序后的链表*/
- return 0;
- }
- List ReverseList(List head)
- {
- if(head->next == NULL || head->next->next == NULL)
- {
- return head; /*链表为空或只有一个元素则直接返回*/
- }
- ListNode *t = NULL,
- *p = head->next,
- *q = head->next->next;
- while(q != NULL)
- {
- t = q->next;
- q->next = p;
- p = q;
- q = t;
- }
- /*此时q指向原始链表最后一个元素,也是逆转后的链表的表头元素*/
- head->next->next = NULL; /*设置链表尾*/
- head->next = p; /*调整链表头*/
- return head;
- }
- void PrintList(List head)
- {
- ListNode* p = head->next;
- while(p != NULL)
- {
- printf("%d ", p->data);
- p = p->next;
- }
- printf("/n");
- }
ZT 链表逆序的更多相关文章
- Reverse Linked List II 单向链表逆序(部分逆序)
0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...
- 链表逆序(JAVA实现)
题目:将一个有链表头的单向单链表逆序 分析: 链表为空或只有一个元素直接返回: 设置两个前后相邻的指针p,q,使得p指向的节点为q指向的节点的后继: 重复步骤2,直到q为空: 调整链表头和链表尾: 图 ...
- 链表逆序,java实现
package com.cskaoyan.linkedlist; //反转数组 public class LinkedListDemo2 { public static Node reverse(No ...
- C# 单向链表 逆序(递归)
static void Main(string[] args) { while (true) { LinkedList L = new LinkedList(); L.Add(new Node(&qu ...
- 链表逆序---python
class ListNode: Value = '' # 节点要储存的值,因为Python是弱类型,因此无需传入泛型 Next = None # 下一个节点,初始化时为空值 def __init__( ...
- 剑指Offer03 逆序输出链表&链表逆序
多写了个逆序链表 /************************************************************************* > File Name: ...
- 基于visual Studio2013解决面试题之0504单链表逆序
题目
- 【云栖社区001-数据结构】如何实现一个高效的单向链表逆序输出(Java版)
如题 动手之前,发现自己很擅长用C语言来写链表. 不过,既然自己做的是Java开发,那么还是用Java实现这个算法吧:毕竟,以后的若干年里都差不多要跟Java打交道了. 于是,先将Java版的链表自学 ...
- C语言两个升序递增链表逆序合并为一个降序递减链表,并去除重复元素
#include"stdafx.h" #include<stdlib.h> #define LEN sizeof(struct student) struct stud ...
随机推荐
- 简述组件化解决方案CTMediator与MGJRouter的主要思想
简述CTMediator CTMediator按照功能的结构来讲,使用时需要实现CTMediator的个三部分. 1.CTMediator类:承担总枢纽,总调度的责任 2.Target_(Modu ...
- Spring AOP 的实现
软件152 余建强 1 使用 API 实现 AOP 新建一个用户接口:UserService package com.cqvie.aop.api; public interface UserServi ...
- 默 of 2018:年终总结
目录 1 概述:在平凡中求变 2 专业分流:一个时代的终点,我的新起点 2.1 我在专业分流前夕的境况 2.2 专业分流情况概述,以及对一篇文章的回顾 2.3 总结与余绪 2.4 附:关于理科与工科的 ...
- 生产环境部署node记录(一)
云服务器厂商:京东云 我选择的操作系统为公共镜像CentOS7.2. 步骤: 首先登陆服务器:使用ssh 用户名@IP地址 登陆 1. wget命令下载Node.js安装包 登陆node的官网复制下 ...
- [javaSE] 集合框架(共性方法)
Collection接口的常用方法 add(),添加一个元素 addAll(),添加一组元素 clear(),清空 remove(),移除一个 removeAll(),移除一组 size(),元素个数 ...
- iOS交互h5——user-agent
User-Agent(用户代理)字符串是Web浏览器用于声明自身型号版本并随HTTP请求发送给Web服务器的字符串,在Web服务器上可以获取到该字符串. 在公司产品中,在userAgent中增加了XX ...
- elasticsearch 6.3 安装手记
系统环境 centos 7 elasticsearch 6.3 需要 JDK 8 版本,先安装 JDK 8. ES6.3 安装地址: https://www.elastic.co/guide/en/e ...
- python数据类型之简单数据类型
变量使用注意事项 慎用小写字母l和大写字母O,因为它们可能被人看成数值1和0. 应使用小写的python变量名. 字符串 在python中,用引号括起来的都是字符串,其中的引号可以是单引号和双引号. ...
- 转:Windows下PHP7安装Redis和Redis扩展phpredis
原文地址:Windows下PHP7安装Redis和Redis扩展phpredis Windows下PHP7安装Redis和Redis扩展phpredis 2016-06-08 17:53:00 标签: ...
- vue生命周期理解
https://segmentfault.com/a/1190000008010666?utm_source=tag-newest