13.反转链表

本文详细介绍了链表反转算法的基本实现,并通过三次不同优化版本的代码展示,阐述了如何提升代码效率与理解深度。从原始实现到利用STL栈优化,再到使用vector和reverse函数简化过程,每一步都旨在帮助开发者深入理解链表操作和优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

反转链表

题目描述

输入一个链表,反转链表后,输出链表的所有元素。
这道题我做了近40分钟,对链表的插入操作不熟悉!
// 12.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stack>
using namespace::std;

struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
	ListNode* ReverseList(ListNode* pHead) {
		if (!pHead) return NULL;

		stack<ListNode*> stack;
		ListNode* pNode = pHead;

		while (pNode) {
			stack.push(pNode);
			pNode = pNode->next;
		}

		ListNode* pNew = stack.top();
		stack.pop();
		ListNode* tmp = pNew;
		while (!stack.empty()) {
			ListNode* node = stack.top();
			node->next = pNew->next;
			pNew->next = node;
			pNew = pNew->next;
			stack.pop();
		}
		//pNew = tmp;
		return tmp;
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	ListNode n1(1);
	ListNode n2(2);
	ListNode n3(3);
	ListNode n4(4);
	ListNode n5(5);

	n1.next = &n2;
	n2.next = &n3;
	n3.next = &n4;
	n4.next = &n5;

	Solution s;
	s.ReverseList(&n1);
	return 0;
}

		while (!stack.empty()) {
			ListNode* node = stack.top();
			node->next = pNew->next;
			pNew->next = node;
			pNew = pNew->next;
			stack.pop();
		}

第二次做:
/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
		if ( pHead == NULL ) return NULL ;
        
        vector<int> vec ;
        ListNode* pNode = pHead ;
        while ( pNode != NULL ) {
            vec.push_back( pNode -> val ) ;
            pNode = pNode -> next ;
        }
        
        ListNode* pNew = new ListNode( vec[ vec.size() - 1 ] ) ;
        ListNode* pCur = pNew ;
        
        for ( int i = vec.size() - 2; i >= 0; -- i ) {
            ListNode* tmp = new ListNode( vec[i] ) ;
            pCur -> next = tmp ;
            pCur = pCur -> next ;
        }
        return pNew ;
    }
};
while循环中我忘了
pNode = pNode -> next ;

。。。下次注意!

第三次做:
/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
		if ( pHead == NULL ) return NULL ;
        
        vector<int> vec ;
        ListNode* pNode = pHead ;
        while ( pNode != NULL ) {
            vec.push_back( pNode->val ) ;
            pNode = pNode->next ;
        }
        
        std::reverse( vec.begin(), vec.end() ) ;
        
        ListNode* pNew = new ListNode( vec[0] ) ;
        ListNode* pCur = pNew ;
        for ( int i = 1; i < vec.size(); ++ i ) {
            ListNode* tmp = new ListNode( vec[i] ) ;
            pCur->next = tmp ;
            pCur = pCur->next ;
        }
        
        return pNew ;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值