两种方法求单链表逆序

1 递归,非常easy

代码:

#include<iostream>  

using namespace std;  

typedef struct node{
	int data;
	struct node * pNext;
}Node ,*pNode;

void createNode(pNode & pHead){
	int temp;
	scanf("%d",&temp);
	pNode p,q;
	bool isFirst = true;
	while(temp){
		if(isFirst){
			p=q=pHead=(pNode)malloc(sizeof(Node));
			pHead->data = temp;
			isFirst = false;
		}else{
			q = new Node();
			q->data = temp;
			q->pNext=NULL;
			p->pNext = q;
			p =q;
		}
		scanf("%d",&temp);
	}

}
void print(pNode pHead){
	if(pHead){
		cout<<pHead->data<<" ";
		print(pHead->pNext);
	}else
	return;
}

void reverse(pNode pcur,pNode & pHead){
	if((NULL == pcur) ||( NULL == pcur->pNext)){
			pHead = pcur;
			return;
	}else{
		pNode pnext = pcur->pNext;
		reverse(pnext,pHead);
		pnext->pNext = pcur;
		pcur->pNext = NULL;
	}
}
int main()
{
	pNode pHead = NULL;
	createNode(pHead);
	cout<<"原链表:"<<endl;
	print(pHead);
	cout<<"翻转:"<<endl;
	reverse(pHead,pHead);
	print(pHead);
    return 0;
}  

执行结果:

2 不是递归,循环:

#include<iostream>  

using namespace std;  

typedef struct node{
	int data;
	struct node * pNext;
}Node ,*pNode;

void createNode(pNode & pHead){
	int temp;
	scanf("%d",&temp);
	pNode p,q;
	bool isFirst = true;
	while(temp){
		if(isFirst){
			p=q=pHead=(pNode)malloc(sizeof(Node));
			pHead->data = temp;
			isFirst = false;
		}else{
			q = new Node();
			q->data = temp;
			q->pNext=NULL;
			p->pNext = q;
			p =q;
		}
		scanf("%d",&temp);
	}

}
void print(pNode pHead){
	if(pHead){
		cout<<pHead->data<<" ";
		print(pHead->pNext);
	}else
	return;
}

pNode reverse(pNode & pHead){
	if(NULL == pHead || NULL == pHead->pNext){
		return pHead;
	}
	pNode p1,p2,p3;
	p1=pHead;
	p2=pHead->pNext;
	while(p2){
		p3=p2->pNext;
		p2->pNext = p1;
		p1 = p2;
		p2 = p3;
	}
	pHead->pNext = NULL; /*设置链表尾*/
	pHead = p1;  /*调整链表头*/
	return pHead;
}
int main()
{
	pNode pHead = NULL;
	createNode(pHead);
	cout<<"原链表:"<<endl;
	print(pHead);
	cout<<"翻转:"<<endl;
	reverse(pHead);
	print(pHead);
    return 0;
}  

执行结果:

时间: 2024-10-26 08:01:34

两种方法求单链表逆序的相关文章

Math01: 两种方法求下三角矩阵的逆

方法一:单位矩阵消元 1 clear; 2 n = 500; 3 A = zeros(n,n); 4 for j = 1:n 5 for i = j:n 6 A(i,j) = (i + j)^2; 7 end 8 end 9 C = A; 10 B = eye(n); 11 12 for i = 1:(n-1) 13 B(i,:) = B(i,:)/A(i,i); 14 A(i,:) = A(i,:)/A(i,i); 15 for j = (i+1):n 16 B(j,:) = B(j,:) -

单链表逆序或者逆序输出

分为两种情况,一种是只逆序输出,实际上不逆序:另一种是把链表逆序. ********************逆序输出*********************** 1 #include<iostream> 2 #include<stack> 3 #include<assert.h> 4 using namespace std; 5 6 7 typedef struct node{ 8 int data; 9 node * next; 10 }node; 11 12 //

JAVA实现两种方法反转单列表

/** * @author luochengcheng * 定义一个单链表 */ class Node { //变量 private int record; //指向下一个对象 private Node nextNode; public Node(int record) { super(); this.record = record; } public int getRecord() { return record; } public void setRecord(int record) { t

华为机试题-- 单链表逆序

[问题] 单链表逆序 [代码] #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct ListNode { int value; struct ListNode *next; }ListNode; typedef struct ListNode *List; List reverseList(List head) //列表逆序 { ListNode *rear, *curr,

两种方法求丑数

我们把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 方法1 : 暴力破解,逐个判断 代码: <pre name="code" class="cpp">#include <iostream> #include <vector> using namespace std; //判断是否是丑数 bool isUgly(int index){ while(index % 2

链表 - 单链表逆序

单链表逆序是经典的链表操作算法,单链表逆序的算法思想是将链表箭头反指(假设next指针是一个箭头),即所谓的改链,改链过程如下. 逆序前: head-->......prev-->cur-->next-->......->NULL 逆序后: NULL<--......prev<--cur<--next<--......head 算法逻辑: 1.空链表或只有一个元素,返回原链表head. 2.定义3个指针prev.cur.next,初始化时,prev指向

单链表逆序的三种方法

一.不使用额外存储空间的逆序 LinkList ReverseLink(LinkList L) { LinkList *next; LinkList *prev = NULL; LinkList *head = L->next; while(head != NULL) { next = head->next; head->next = prev; prev = head; head = next; } L->next = prev return L; } 二.头插法逆序 LinkL

HDU 1013 Digital Roots(两种方法,求数字根)

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 67949    Accepted Submission(s): 21237 Problem Description The digital root of a positive integer is found by summing the digits of

单链表逆序的几种方法

假设单链表数据结构定义如下: struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; 单链表有一个头指针指向第一个结点,最后一个结点指向NULL 一.最容易想到的方法,新建一个单链表newNode,每次将原先链表的第一个结点放到newNode后 ListNode* reverseList(ListNode* head) { ListNode *newNode = new ListN