积少成多

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

链表节点定义:

class ListNode///单链表节点
{
public:
    int val;
    ListNode *next;
    ListNode(int x): val(x),next(nullptr) {}
};

struct RandomListNode{///随机链表节点
        int label;
        RandomListNode *next,*random;
        RandomListNode(int x):label(x),next(nullptr),random(nullptr){}
};

class TreeNode {///树节点表示
    public:
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x): val(x),left(nullptr),right(nullptr){}
};

就地逆置

ListNode* revOnPlace(ListNode *head)
    {
        ListNode dummy(-1);//假的头节点
        ListNode *curr = head;

        head = &dummy;
        while(curr)
        {
            ListNode *tmp = curr->next;
            curr->next = head->next;
            head->next = curr;
            curr = tmp;
        }

        return dummy.next;
    }

 

posted on 2016-06-22 13:49  x7b5g  阅读(290)  评论(0编辑  收藏  举报