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





    #include "stdafx.h"
    #include <string>
    using namespace std;
    #include <vector>
    #include <stack>

    typedef struct tag_listnode
    {
        int data;
        struct  tag_listnode *next;

    }listnode;

    class Solution
    {
    public:

        listnode * reseveListnode(listnode *head)
        {
            listnode *node = head;
            listnode *next_node = head->next;

            while (node !=nullptr)
            {
                listnode *temp = next_node->next;  // 保存要断开的节点的下一个阶段,为了保持链表的遍历
                next_node->next = node;            // 翻转链表,将下个节点指向当前节
                //当前节点的头部为null、所以这里nextNode 变成 12-->null
                //同理下一次变成21->12->null
                node = next_node;                  // 更新当前节点变量
                next_node = temp;                  // 更新下个节点变量
            }
            head->next = NULL;                    // 将头部的next指向空,因为当前已经为最后的节点
            return node;
        }

    };


    int main()
    {
        listnode head, node1, node2, node3, node4;
        node1.data = 11;  node1.next = &node2;
        node2.data = 22; node2.next = &node3;
        node3.data = 33; node3.next = &node4;
        node4.data = 44; node4.next = nullptr;
        head.next = &node1;
        Solution sou;
        listnode *result;
        result = sou.reseveListnode(&head);
        return 1;
    }

    天天向上
  • 相关阅读:
    316 Remove Duplicate Letters 去除重复字母
    315 Count of Smaller Numbers After Self 计算右侧小于当前元素的个数
    313 Super Ugly Number 超级丑数
    312 Burst Balloons 戳气球
    309 Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
    Java 类成员的初始化顺序
    JavaScript 全局
    HTML字符实体
    Java中的toString()方法
    JavaScript 弹窗
  • 原文地址:https://www.cnblogs.com/hg07/p/12728109.html
Copyright © 2011-2022 走看看