单链表逆转 ( 含例题 Reversing Linked List )

单链表逆转模板 

Ptr Reverse (Ptr head,int K){
	cnt=1;
	new=head->next;
    old=new->next;
    while(cnt<K){
        tmp=old->next;
        old->next=new;
        new=old;
        old=tmp;
        cnt++;
    }
    head->next->next=old;
    return new;
}

//定义性声明均省略

new指针指向已经逆转好的链表,

old指针指向还未逆转好的链表,

tmp指针暂存链表地址防止数据丢失。

例题    Reversing Linked List

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address  Data  Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

Demo:

#include<iostream>
#include<string>
using namespace std;
typedef struct node* nodePtr;
struct node {
	string address;
	int data;
	string nextAddress;
	nodePtr next;
};
nodePtr Reverse(nodePtr head, int K, int N) {
	int cnt = 1;
	nodePtr newPtr = NULL, oldPtr = NULL, tmpPtr = NULL, headPtr = NULL;
	for (int i = 0; i < N / K; i++) {
		cnt = 1;
		if (i==1) head = head->next;
        newPtr = head->next;
		oldPtr = newPtr->next;
		while (cnt < K) {
			tmpPtr = oldPtr->next;
			oldPtr->next = newPtr;
			oldPtr->nextAddress = newPtr->address;
			newPtr = oldPtr;
			oldPtr = tmpPtr;
			cnt++;
		}
		if (!i) headPtr = newPtr;
		(head->next)->next = oldPtr;
		if (oldPtr != NULL) {
			(head->next)->nextAddress = oldPtr->address;
		}
		else (head->next)->nextAddress = -1;
		if (i) {
            tmpPtr = head;
            if(i>=1) head=head->next;
			tmpPtr->next =newPtr;
			tmpPtr->nextAddress = newPtr->address;
		}
        
	}
	return headPtr;
}
int main() {
	int n, reverse;
	int cnt=1;
	string headAddress;
	cin >> headAddress >> n >> reverse;
	node node[n+1];
	nodePtr HeadPtr=NULL, NewHead=NULL;
	for (int i = 1; i <=n; i++) {
		cin >> node[i].address >> node[i].data >> node[i].nextAddress;
	}
	if(n==1){
		cout << node[1].address << " " << node[1].data << " " << "-1" << endl;
        return 0;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (node[i].nextAddress == node[j].address){
				node[i].next = &node[j];
				cnt++;
				break;
			}
		}
		if (headAddress == node[i].address){
			node[0].next = &node[i];
			HeadPtr = &node[0];
		}
		if (node[i].nextAddress == "-1") node[i].next = NULL;
	}
	if(reverse == 1)    NewHead = HeadPtr->next;
	else	NewHead = Reverse(HeadPtr, reverse, cnt);
	while (1) {
		cout << NewHead->address << " " << NewHead->data << " " << NewHead->nextAddress << endl;
		if (NewHead->next == NULL) break;
		NewHead = NewHead->next;
	}
	return 0;
}

//使用string存储当前地址和下一地址值

优化:

以上代码中,连接链表时使用了两层for循环,时间复杂度为O(n^2),当n值较大时,易出现运行时间过长的问题。因此可以将数组的下标作为链表的地址值,在创建新链表时即可将链表连接起来。同时运用iomanip头文件中的setfill函数和setw函数方便地址值的输出。

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
typedef struct node* nodePtr;
struct node {
	int address;
	int data;
	int nextAddress;
	nodePtr next;
};
nodePtr Reverse(nodePtr head, int K, int N) {
	int cnt = 1;
	nodePtr newPtr = NULL, oldPtr = NULL, tmpPtr = NULL, headPtr = NULL;
	for (int i = 0; i < N / K; i++) {
		cnt = 1;
		if (i==1) head = head->next;
        newPtr = head->next;
		oldPtr = newPtr->next;
		while (cnt < K) {
			tmpPtr = oldPtr->next;
			oldPtr->next = newPtr;
			oldPtr->nextAddress = newPtr->address;
			newPtr = oldPtr;
			oldPtr = tmpPtr;
			cnt++;
		}
		if (!i) headPtr = newPtr;
		(head->next)->next = oldPtr;        //实现反向链表和正向链表的连接
		if (oldPtr != NULL) {
			(head->next)->nextAddress = oldPtr->address;
		}
		else (head->next)->nextAddress = -1;
		if (i) {
            tmpPtr = head;
            if(i>=1) head=head->next;      //将head指针指向反向链表的最后一个
			tmpPtr->next =newPtr;          //实现当前反向链表段与上一反向链表段的连接
			tmpPtr->nextAddress = newPtr->address;
		}
        
	}
	return headPtr;      //返回头节点
}
int main() {
	int n, reverse;
	int cnt = 0;
	int headAddress;
	int Index;
	cin >> headAddress >> n >> reverse;
	node node[100005];
	nodePtr HeadPtr = NULL, NewHead = NULL;
	for (int i = 1; i <= n; i++) {           //node[0]作为空链表
		cin >> Index;
		node[Index + 1].address = Index;
		cin >> node[Index + 1].data >> node[Index + 1].nextAddress;
        if (node[Index + 1].nextAddress == -1) node[Index+1].next = NULL;
		else node[Index + 1].next = &node[node[Index + 1].nextAddress + 1];
		
	}
	node[0].next = &node[headAddress + 1];
	HeadPtr = &node[0];
	while(HeadPtr->next!= NULL){
		HeadPtr=HeadPtr->next;
		cnt++;
	}
	HeadPtr = &node[0];
	if (n == 1) {
		cout << setfill('0');
		cout << setw(5) << HeadPtr->next->address << " " << HeadPtr->next->data << " " << "-1" << endl;
		return 0;
	}
	if (reverse == 1)    NewHead = HeadPtr->next;
	else	NewHead = Reverse(HeadPtr, reverse, cnt);
	while (1) {
		cout << setfill('0');
        if(NewHead->nextAddress!=-1  )
                cout << setw(5) << NewHead->address << " " << NewHead->data << " " << setw(5) << NewHead->nextAddress << endl;
		else    cout << setw(5) << NewHead->address << " " << NewHead->data << " " << NewHead->nextAddress << endl;
		if (NewHead->next == NULL) break;
		NewHead = NewHead->next;
	}
	return 0;
}

//使用int存储当前地址和下一地址值(00100存储为int类型后值为100)
//利用setfill函数和setw函数在int类型前添加0以实现地址值的输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值