美文网首页
作业三——反转链表

作业三——反转链表

作者: MaosongRan | 来源:发表于2018-10-08 20:57 被阅读0次

题目

用代码实现反转链表,要求:不能使用栈数据结构,时间复杂度O(n).

实现

  • LinkList.h文件代码
#ifndef _LINKLIST_H_
#define _LINKLIST_H_

// 节点类, 使用了模板类,方便数据类型
template<class T>
class Node
{
public:
    T data;
    Node<T>* next;
public:
    Node(){}
    Node(T v, Node<T>* node)
    {
        this->data = v;
        this->next = node;
    }
};

template<class T>
class LinkList
{
private:
    Node<T>* head;
    Node<T>* tail;
    int count;
public:
    LinkList();
    ~LinkList();
    int size();
    void print();
    void append(T data);
    void reverse();
};

// 初始化链表
template<class T>
LinkList<T>::LinkList() : count(0)
{
    head = new Node<T>;
    head->next = NULL;
    tail = head;
    int length;
    cout << "请输入链表初始长度:";
    cin >> length;
    for (int i = 0; i < length; ++i)
    {
        T data;
        cout << "第" << (i + 1) << "个节点的内容:";
        cin >> data;
        append(data);
    }
}

template<class T>
LinkList<T>::~LinkList()
{
    Node<T>* node = head->next;
    Node<T>* tmp;
    while (node != NULL)
    {
        tmp = node;
        node = node->next;
        delete tmp;
    }
    delete head;
    head = NULL;
    tail = NULL;
}

// 返回链表的长度
template<class T>
int LinkList<T>::size()
{
    return count;
}

template <class T>
void LinkList<T>::print()
{
    Node<T>* node = head->next;
    while (node)
    {
        cout << node->data << "  ";
        node = node->next;
    }
    cout << endl;
}

template <class T>
void LinkList<T>::append(T data)
{
    Node<T>* node = new Node<T>();
    node->data = data;
    node->next = NULL;
    tail->next = node;
    tail = node;
    ++count;
}

template <class T>
void LinkList<T>::reverse()
{
    // 由于是带有头节点的链表,因此需要这两个条件
    if (head->next == NULL || head->next->next == NULL)
        return;
    Node<T>* current = head->next->next;
    tail = head->next;
    tail->next = NULL;
    while (current)
    {
        Node<T>* tmp = current->next;
        current->next = head->next;
        head->next = current;
        current = tmp;
    }
}
#endif
  • main.cpp
#include <iostream>
#include "LinkList.cpp"
using namespace std;

int main()
{
    LinkList<int> list = LinkList<int>();
    cout << "链表结构内容:";
    list.print();
    list.reverse();
    cout << "链表反转后的结构:";
    list.print();
    system("pause");
    return 0;
}

相关文章

  • 作业三——反转链表

    题目 用代码实现反转链表,要求:不能使用栈数据结构,时间复杂度O(n). 实现 LinkList.h文件代码 ma...

  • Algorithm小白入门 -- 单链表

    单链表递归反转链表k个一组反转链表回文链表 1. 递归反转链表 单链表节点的结构如下: 1.1 递归反转整个单链表...

  • 链表反转

    循环反转链表 递归反转链表

  • 5个链表的常见操作

    链表 链表反转 LeetCode206:给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。 环路检...

  • JZ-015-反转链表

    反转链表 题目描述 输入一个链表,反转链表后,输出新链表的表头。题目链接: 反转链表[https://www.no...

  • 算法学习(链表相关问题)

    LeetCode 206 反转链表 LeetCode 92 反转链表II (练习) 完成,方法:在反转链表上改 L...

  • 实战高频leetcode题目

    1. 反转链表 : 反转链表是常见简单题目,定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点...

  • 剑指offer-15-反转链表

    反转链表: 输入一个链表,反转链表后,输出新链表的表头。 思路:三指针abc一次排列,每次都将b指向a,然后各推进...

  • 【教3妹学算法】2道链表类题目

    题目1:反转链表 给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。 示例 1: 输入:head ...

  • algrithrom

    求和问题,双指针解决 done 两数之和 三数之和 最接近三数之和 四数之和 链表反转问题 done 链表反转 链...

网友评论

      本文标题:作业三——反转链表

      本文链接:https://www.haomeiwen.com/subject/rbxsaftx.html