zoukankan      html  css  js  c++  java
  • 递归、非递归 反转单链表

    定义链表结构

    struct ListNode
    {
        int val;
        ListNode *next;
        ListNode(int v) : val(v), next(NULL) {}
    };

    非递归反转单链表

    ListNode* reverse(ListNode *root)
    {
        if (root == NULL || root->next == NULL)
            return root;
        ListNode *cur = root->next;
        root->next = NULL;
        while (cur != NULL)
        {
            ListNode *tmp = cur->next;
            cur->next = root;
            root = cur;
            cur = tmp;
        }
        return root;
    }

    递归反转单链表

    void reverseRec(ListNode *root, ListNode *&head)
    {
        if (root == NULL)
            return;
        if (root->next == NULL)
        {
            head = root;
            return;
        }
        reverseRec(root->next, head);
        root->next->next = root;
        root->next = NULL;
    }

    测试

    #include <iostream>
    using namespace std;
    struct ListNode
    {
        int val;
        ListNode *next;
        ListNode(int v) : val(v), next(NULL) {}
    };
    ListNode* createList(ListNode *root)
    {
        if (root == NULL)
        {
            ListNode *root = new ListNode(0);
            ListNode *p1 = new ListNode(1);
            ListNode *p2 = new ListNode(2);
            root->next = p1;
            p1->next = p2;
            return root;
        }
    }
    void tranverse(ListNode *root)
    {
        while (root != NULL)
        {
            cout << root->val << " ";
            root = root->next;
        }
        cout << endl;
    }
    void deleteList(ListNode *root)
    {
        while (root != NULL)
        {
            delete root;
            root = NULL;
        }
    }
    ListNode* reverse(ListNode *root)
    {
        if (root == NULL || root->next == NULL)
            return root;
        ListNode *cur = root->next;
        root->next = NULL;
        while (cur != NULL)
        {
            ListNode *tmp = cur->next;
            cur->next = root;
            root = cur;
            cur = tmp;
        }
        return root;
    }
    void reverseRec(ListNode *root, ListNode *&head)
    {
        if (root == NULL)
            return;
        if (root->next == NULL)
        {
            head = root;
            return;
        }
        reverseRec(root->next, head);
        root->next->next = root;
        root->next = NULL;
    }
        
    int main()
    {
        ListNode *root = createList(root);
        //ListNode *root = NULL; 
        tranverse(root);
        root = reverse(root);
        tranverse(root);
    
        ListNode *head = NULL;
        reverseRec(root, head);
        tranverse(head);
        
        deleteList(head);
    }
    View Code
  • 相关阅读:
    HDU 3401 Trade
    POJ 1151 Atlantis
    HDU 3415 Max Sum of MaxKsubsequence
    HDU 4234 Moving Points
    HDU 4258 Covered Walkway
    HDU 4391 Paint The Wall
    HDU 1199 Color the Ball
    HDU 4374 One hundred layer
    HDU 3507 Print Article
    GCC特性之__init修饰解析 kasalyn的专栏 博客频道 CSDN.NET
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/3955101.html
Copyright © 2011-2022 走看看