首页 > 其他分享 >Reverse a linked list【1月17日学习笔记·】

Reverse a linked list【1月17日学习笔记·】

时间:2024-01-17 15:59:20浏览次数:29  
标签:node run Reverse 17 list next current NULL 节点

点击查看代码
//Reversse a linked list
#include<iostream>
using namespace std;
struct node {
	int data;
	node* next;
};
node* A;

void reverse() {
	node* next;//用于保存下一个·节点地址以便索引
	node* current = A;//当前索引
	node* prev = NULL;//保存上一个节点地址

	while (current != NULL) {
		next = current->next;//指向下一节点
		current->next = prev;//当前节点尾巴指向上一节点
		prev = current;//更新,指向当前节点
		current = next;//索引指向下一节点
	}
	A = prev;//头指针指向末节点
}//头指针为空或只有一个节点时仍适用

void insert(int x) {
	node* temp = new node;
	temp->data = x;
	temp->next = NULL;

	if (A == NULL) {
		A = temp;
	}//头指针为空时要建立globle区与heap区的link才能通过头指针索引
	else {
		node* run = A;
		while (run->next != NULL) {
			run = run->next;
		}
		run->next = temp;
	}//头指针不为空时用遍历指针索引
}

void print() {
	node* run = A;
	while (run != NULL) {
		cout << run->data << " ";
		run = run->next;
	}
	cout << endl;
}

int main() {
	A = NULL;
	insert(2);
	insert(4);
	insert(6);
	insert(8);
	print();
	reverse();
	print();
}

标签:node,run,Reverse,17,list,next,current,NULL,节点
From: https://www.cnblogs.com/whvivy/p/17970186

相关文章

  • [CF1707E] Replace
    Replace题面翻译题目描述给定一个长为\(n\)的序列\(a_1,\ldots,a_n\),其中对于任意的\(i\)满足\(1\leqa_i\leqn\)。定义一个二元组函数如下:\[f((l,r))=(\min\{a_l,\ldots,a_r\},\max\{a_l,\ldots,a_r\})(l\leqr)\]你需要回答\(q\)次询问,每次给定\((l_i,r_i)\)......
  • Delete d node at nth position【1月17日学习笔记】
    点击查看代码//Deletednodeatnthposition#include<iostream>usingnamespacestd;structnode{ intdata; node*next;};node*A;voidinsert(intx){ node*temp=newnode; temp->data=x; temp->next=NULL; if(A==NULL){ A=temp;......
  • (Python)每日代码||2024.1.17||函数中给列表形参默认值时,该默认列表在函数中的改变会
    deff(x,li=[1]):print(id(li))li.append(x)print(li)f('a')#第一次调用函数print()f('b')#第二次调用函数print()f('a',[])#第三次调用函数print()f('b',[2,2])#第四次调用函数print()f('a')#第五次调用函数'''输出14......
  • Inserting a node at nth position【1月17日学习笔记】
    点击查看代码//Insertinganodeatnthposition#include<iostream>usingnamespacestd;structnode{intdata;node*next;};node*A;//全局头指针voidinsert(intx,intn){node*temp=newnode;//temp是局部变量,在stack区,每次调用更新temp-......
  • 运城学院数学与信息技术学院 2017—2018学年第二学期期末考试
    运城学院数学与信息技术学院2017—2018学年第二学期期末考试程序设计基础试题(A)适用范围:计算机科学与技术专业1701\1702班网络工程专业1703\1704\1705班信息管理与信息系统专业1706班数字媒体技术专业1707\1708班通信工程专业1709\17010班 命题人: 南丽丽       ......
  • 【240117-1】如图,A和B为两正方形,两者共一顶角。求证:顶角两侧三角线面积相等。
    ......
  • P9017 [USACO23JAN] Lights Off G 题解
    一次操作相当于把\(a\)异或上\(b\),修改开关的一位相当于将这一位异或上\(1\)。会发现一个很神奇的性质:初始开关对灯的影响和改变开关状态对灯的影响是独立的。而前者的影响是固定的,所以我们可以只考虑改变开关状态对灯的影响。假设一共需要\(k\)次操作能使所有灯关闭,如果我......
  • 17类模板
    类模板类成为类名和类型参数的组合无论是一般类还是模板类,只有调用到的成员函数,才会出现在符号表上。#pragmaonce#include<iostream>#include<cstring>usingnamespacestd;template<typenameT>classSeqStack{//模板名称+类型参数列表=类名称private: T*......
  • 性能篇:List集合遍历元素用哪种方式更快?
    嗨大家好,我是小米!今天给大家分享一篇关于Java集合框架性能的文章,话题是:“如果让你使用for循环以及迭代循环遍历一个ArrayList,你会使用哪种方式呢?原因是什么?LinkedList呢?”废话不多说,让我们直入主题!ArrayList的get元素源码介绍ArrayList,作为Java集合框架中的一个重要类,是基于数组......
  • CentOS7 报错 ”Repository base is listed more than once in the configuration...
    CentOS7在使用yum时出现以下错误:RepositorybaseislistedmorethanonceintheconfigurationRepositoryupdatesislistedmorethanonceintheconfigurationRepositoryextrasislistedmorethanonceintheconfigurationRepositorycentosplusislistedmore......