编程练习- 链表题目-反序,合并
Code Snippet
- #include <iostream>
- #include<fstream>
- using namespace std;
- struct Node
- {
- int data ;
- Node *next ;
- };
- typedef struct Node Node ;
- void ReverseList(Node*& head)
- {
- if(head == NULL || head->next == NULL)
- return;
- Node* currentNode = head;
- Node* previousNode = NULL;
- while(currentNode != NULL)
- ...{
- Node* temp = currentNode->next;
- currentNode->next = previousNode;
- previousNode = currentNode;
- currentNode = temp;
- }
- head = previousNode;
- }
- // My answer.
- Node* MergeLinkList(Node* listA, Node* listB)
- {
- if(listA == NULL)
- return listB;
- if(listB == NULL)
- return listA;
- Node* pHeadSmall = listA -> data < listB -> data ? listA : listB;
- Node* pHeadBig = pHeadSmall == listA ? listB : listA;
- Node* p = pHeadSmall;
- Node* q = pHeadSmall->next;
- Node* r = pHeadBig;
- while(q != NULL && r != NULL)
- ...{
- if(q->data <= r->data)
- ...{
- p->next = q;
- p = p->next;
- q = q->next;
- }
- else
- ...{
- p->next = r;
- p = p->next;
- r = r->next;
- }
- }
- if(q == NULL)
- {
- p->next = r;
- }
- else
- {
- p->next = q;
- }
- return pHeadSmall;
- }
- // Standard answer
- Node * Merge(Node *head1 , Node *head2)
- {
- if ( head1 == NULL)
- return head2 ;
- if ( head2 == NULL)
- return head1 ;
- Node *head = NULL ;
- Node *p1 = NULL;
- Node *p2 = NULL;
- if ( head1->data < head2->data )
- {
- head = head1 ;
- p1 = head1->next;
- p2 = head2 ;
- }
- else
- {
- head = head2 ;
- p2 = head2->next ;
- p1 = head1 ;
- }
- Node *pcurrent = head ;
- while ( p1 != NULL && p2 != NULL)
- {
- if ( p1->data <= p2->data )
- {
- pcurrent->next = p1 ;
- pcurrent = p1 ;
- p1 = p1->next ;
- }
- else
- {
- pcurrent->next = p2 ;
- pcurrent = p2 ;
- p2 = p2->next ;
- }
- }
- if ( p1 != NULL )
- pcurrent->next = p1 ;
- if ( p2 != NULL )
- pcurrent->next = p2 ;
- return head ;
- }
- // Utillity function.
- void TestList(Node* head)
- {
- Node* p = head;
- while(p != NULL)
- {
- cout << p->data << endl;;
- p= p->next;
- }
- }
- // Utillity function.
- Node* InputList(ifstream& inFile)
- {
- int len;
- inFile >> len;
- Node* head = new Node[len];
- Node* p = head;
- for(int i = 0; i < len - 1; i++)...{
- inFile >> p->data;
- p->next = p + 1;
- p = p->next;
- }
- inFile >> p->data;
- p->next = NULL;
- return head;
- }
- int main()
- {
- ifstream inFile("d:\data.txt");
- if(!inFile)
- return -1;
- Node* headA = InputList(inFile);
- Node* headB = InputList(inFile);
- TestList(headA);
- TestList(headB);
- ReverseList(headA);
- cout<< "Testing Reverse A" << endl;
- TestList(headA);
- ReverseList(headA);
- cout<< "Testing Reverse A back." << endl;
- TestList(headA);
- Node* newHead = Merge(headA, headB);
- cout<< "Testing Merged List." << endl;
- TestList(newHead);
- while(newHead != NULL)
- {
- Node* ptr = newHead;
- newHead = newHead->next;
- delete ptr;
- }
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律