zoukankan      html  css  js  c++  java
  • 单向链表的反转

    #include <iostream>
    #include <list>
    #include <vector>
    
    using namespace std;
    
    class ListNode {
    public:
    	ListNode *next;
    	int data;
    };
    
    ListNode* reverse(ListNode * head) {
    
    	ListNode *pCur, *pPrev, *pNext;
    
    	pPrev = NULL;
    	pCur = head;
    
    	while (pCur != NULL) {
    		pNext = pCur->next;
    		pCur->next = pPrev;
    		pPrev = pCur;
    		pCur = pNext;
    	}
    
    	return pPrev;
    }
    
    void travelList(ListNode* head) {
    	ListNode *cur = head;
    	while (cur != NULL) {
    		cout << "node: " << cur->data << endl;
    		cur = cur->next;
    	}
    }
    
    int main() {
    
    	ListNode *head = NULL, *cur = NULL;
    
    	for (int i = 0; i < 10; i++) {
    		ListNode * temp = new ListNode;
    		temp->data = i;
    		temp->next = NULL;
    		if (cur == NULL) {
    			cur = temp;
    			head = temp;
    		} else {
    			cur->next = temp;
    			cur = temp;
    		}
    	}
    
    	cout << "beafore reverse:" << endl;
    	travelList(head);
    	cout << "================== end =================" << endl;
    
    	cout << "after reverse:" << endl;
    	cur = reverse(head);
    	travelList(cur);
    	cout << "================== end =================" << endl;
    
    	vector<int> testV;
    	list<int> testL;
    
    	return 0;
    }
    

      

  • 相关阅读:
    C#基本语法
    C#程序结构
    C#环境
    C#强大的编程功能
    Razor
    Web Pages
    ASP.NET教程
    [ecmagnet][django] 如何使用django的signal
    [ecmanget][常用标签]bookmark
    [muku][1 初始restful api] chorme安装jsonview 插件
  • 原文地址:https://www.cnblogs.com/Hali-yang/p/3402703.html
Copyright © 2011-2022 走看看