设计需求,把链表倒序头尾颠倒。
需求简单,代码简单,直接黏贴。
Java同理
节点头文件
#pragma once
class Node
{
private:
int id;
Node *next;
public:
Node(int id);
~Node(void);
Node* getNextNode();
void setNextNode(Node*);
void sayId();
int getId();
};
节点实现
#include "Node.h"
#include <iostream>
using namespace std;
Node::Node(int id)
{
this->id = id;
this->next = NULL;
}
Node::~Node(void)
{
}
Node* Node::getNextNode()
{
return this->next;
}
void Node::setNextNode(Node *next)
{
this->next = next;
}
void Node::sayId()
{
cout<<this->id<<endl;
}
int Node::getId()
{
return this->id;
}
倒序函数
Node* reverseNode(Node* head)
{
if (head->getNextNode() == NULL)
{
return head;
}
Node* phead =