《剑指offer》面试题6:从尾到头打印链表

简单的单向链表:

面试题6:从尾到头打印链表

面试题18:删除链表节点

面试题22:链表中倒数第k个节点

面试题24:“反转链表”

面试题25:“合并两个排序的链表”

面试题52:两个链表的第一个公共点


题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值。

分析:遍历链表的顺序是从头到尾,可输出的顺序是从尾到头。即,后进先出。

(1)经过一个节点,吧节点放到一个栈中。遍历完整个链表,再从栈顶逐个输出节点的值,此时输出节点的顺序反转。

(2)实现反转链表,每访问一个节点,先递归输出它后面的节点,再输出该节点自身,此时链表输出结果反转。

       (递归的本质就是一个栈结构,递归实现)

 

插入空链表 -- 插入节点即链表头指针。头指针会改动,把pHead参数设为指向指针的指针,否则仍是控制帧。

查找 i 节点 -- 遍历链表,时间复杂度为 O(n);数组,时间复杂度 O(1)

#include "..\ConsoleApplication3\List.h"
#include <stack>
#include <iostream>

void PrintListReversingly_Iteratively(ListNode* pHead) //遍历链表
{
	std::stack<ListNode*> nodes;  //初始化一个栈

	ListNode* pNode = pHead;  //节点
	while (pNode != nullptr)  //节点不为空
	{
		nodes.push(pNode);  //进栈
		pNode = pNode->m_pNext;
	}

	while (!nodes.empty())  //栈不空
	{
		pNode = nodes.top();  //栈顶
		std::cout << pNode->m_nValue << std::endl;
		nodes.pop();  //出栈
	}
}

void PrintListReversingly_Recursively(ListNode* pHead)
{
	if (pHead != nullptr)
	{
		if (pHead->m_pNext != nullptr)
		{
			PrintListReversingly_Recursively(pHead->m_pNext);
		}
		std::cout << pHead->m_nValue << std::endl;
	}
}

int main()
{
	/*
	PrintList(pHead);
	PrintListReversingly_Iteratively(pHead);
	std::cout << " " << std::endl;
	PrintListReversingly_Recursively(pHead);
	*/
	return 0;
}

头文件:

List.h

struct ListNode   // 链表节点定义
{
	int       m_nValue; //值
	ListNode* m_pNext;  //
};

__declspec(dllexport) ListNode* CreateListNode(int value);
__declspec(dllexport) void ConnectListNode(ListNode* pCurrent, ListNode* pNext);
__declspec(dllexport) void PrintListNode(ListNode* pNode);
__declspec(dllexport) void PrintList(ListNode* pHead);
__declspec(dllexport) void DestroyList(ListNode* pHead);
__declspec(dllexport) void AddToTail(ListNode** pHead, int value);//链表末尾添加一个节点
__declspec(dllexport) void RemoveNode(ListNode** pHead, int value); //找到第一个含有某值的节点并删除

List.cpp

#include "list.h"
#include <stdio.h>
#include <stdlib.h>

ListNode* CreateList(int value) //创建链表
{
	ListNode* pNode = new ListNode();
	pNode->m_nValue = value;
	pNode->m_pNext = nullptr;

	return pNode;
}

void ConnectListNodes(ListNode* pCurrent, ListNode* pNext) //当前节点链接的下一个节点
{
	if (pCurrent == nullptr)
	{
		printf("Error to connect two nodes.\n");
		exit(1);
	}
	pCurrent->m_pNext = pNext;
}
void PrintListNode(ListNode* pNode) //打印链表节点
{
	if (pNode == nullptr)
		printf("The node is nullptr\n");
	else
		printf("The key in node is %d.\n",pNode->m_nValue);
}

void PrintList(ListNode* pHead)
{
	printf("PrintList start.\n");

	ListNode* pNode = pHead;
	while (pNode != nullptr)
	{
		printf("%d\t",pNode->m_nValue);
		pNode = pNode->m_pNext;
	}
	printf("\nPrintList ends. \n");
}
void DestroyList(ListNode* pHead) //删除节点
{
	ListNode* pNode = pHead;
	while (pNode != nullptr)
	{
		pHead = pHead->m_pNext;
		delete pNode;
		pNode = pHead;
	}
}

void AddToTail(ListNode** pHead, int value) //链表末尾添加一个节点
{
	ListNode* pNew = new ListNode();
	pNew->m_nValue = value;
	pNew->m_pNext = nullptr;

	if (*pHead == nullptr)
	{
		*pHead = pNew;
	}
	else
	{
		ListNode* pNode = *pHead;
		while (pNode->m_pNext!=nullptr)
		{
			pNode = pNode->m_pNext;
		}
		pNode->m_pNext = pNew;
	}
}


void RemoveNode(ListNode** pHead, int value) //找到第一个含有某值的节点并删除
{
	if (pHead == nullptr || *pHead == nullptr)
		return;

	ListNode* pToBeDeleted = nullptr;
	if ((*pHead)->m_nValue == value)
	{
		pToBeDeleted = *pHead;
		*pHead = (*pHead)->m_pNext;
	}
	else
	{
		ListNode* pNode = *pHead;
		while (pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue != value)
			pNode = pNode->m_pNext;

		if (pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue == value)
		{
			pToBeDeleted = pNode->m_pNext;
			pNode->m_pNext = pNode->m_pNext->m_pNext;
		}
	}

	if (pToBeDeleted != nullptr)
	{
		delete pToBeDeleted;
		pToBeDeleted = nullptr;
	}
}

__declspec(dllexport)用于Windows中的动态库中,声明导出函数、类、对象等供外面调用,省略给出.def文件。即将函数、类等声明为导出函数,供其它程序调用,作为动态库的对外接口函数、类等。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值