【C++获取单链表的倒数第k个节点】教程文章相关的互联网学习教程文章

C++封装链表 类【代码】

List.h #ifndef LIST_H #define LIST_H #include<stdio.h> #include<iostream> #define TYPE int using namespace std; class Node { public:TYPE data;Node* next;Node(TYPE _data){data = _data;next = NULL;} }; class List { public:Node* head;Node* tail;size_t _size;List(void){head = NULL;tail = NULL;_size = 0;}~List(void);//头添加void head_add(TYPE data);//void tail_add(TYPE data);//bool head_del(void);//boo...

Leetcode 141. 环形链表 解题思路及C++实现

解题思路: 定义快慢两个指针,当指针所指节点是同一个时,说明出现了环。 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:bool hasCycle(ListNode *head) {if(!head || !(head->next)) return false;ListNode* fast = head;ListNode* slow = head;while(fast && fast->next){slow = slow->next;f...

leetcode 148. 排序链表(c++)【代码】

在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。 示例 1: 输入: 4->2->1->3输出: 1->2->3->4示例 2: 输入: -1->5->3->4->0输出: -1->0->3->4->5/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode* sortList(ListNode* head) {if (!head || !head->next) return head;ListNo...

C++算法 链表专题【代码】

第一题:https://www.nowcoder.com/practice/0cff324157a24a7a8de3da7934458e34?tpId=98&tqId=32912&tPage=5&rp=5&ru=/ta/2019test&qru=/ta/2019test/question-ranking 找出单向链表中的一个节点,该节点到尾指针的距离为K。链表的倒数第0个结点为链表的尾指针。要求时间复杂度为O(n)。 链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNext; } 链表节点的值初始化为1,2,3,4,5,6,7。 输入描述:该节点...

单链表(C++)【代码】

原文链接:http://www.cnblogs.com/zhuy/archive/2012/03/06/2382380.html 1 #include<iostream> 2 using namespace std; 3 template<class T> 4 struct Node 5 { 6 T data; 7 Node<T> *next; 8 }; 9 template<class T> 10 class LinkList 11 { 12 public: 13 LinkList(); 14 LinkList(T a[],int n); 15 ~LinkList(); 16 int length(); 17 T Get(int i); 18 ...

c++ 实现链式栈(用链表做的栈)【代码】

*****************************.h文件**********************#ifndef LIST_STACK_LISTSTACK_H #define LIST_STACK_LISTSTACK_H //不带表头(数据域为空)的节点,节点first 开始为0template <class T> class liststack;template <class T> class listnode {friend class liststack<T>; private:listnode(T a):data(a),next(0){};T data;listnode<T>* next; }; template <class T> class liststack{ public:liststack():first(0){};...

leetcode 合并K个排序链表 c++实现【代码】

??这道题耗时40ms,思路是联想到快慢指针,vector有几个元素,我也有几个头指针,若A的头指针最小,就放入结果指针head的next中,则A就比别的指针快一步,移到下一位next。问题就是判断如何最小,可以自己写,也可以使用priority_queue这样有序的数据结构。感谢博客最下面的链接,我也学到了如何自定义比较器。代码如下: struct cmp {bool operator()(ListNode *&a, ListNode *&b) const {return a->val > b->val;} }; class Solu...

C++链表简单的应用【代码】

学生管理系统,输入学生的姓名和学号,然后再输出:#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include <stdlib.h>typedef struct STU// 用typedef 来进行取别名 {int num;//学号char name[20];//名字struct STU *pnext;//这个指针指向下一个节点 }STU ; //以上仅仅是对的声明,并没有在内存中分配空间//创建一个数据链表 //创建一个头节点 STU* CreatList() {STU* P = (STU*)malloc(sizeof(STU));//新建一个节点P->...

C/C++笔记1:链表简介&运用【图】

1.为什么要用到链表 数组作为存放同类数据的集合,给我们在程序设计时带来很多的方便,增加了灵活性。但数组也同样存在一些弊病。如数组的大小在定义时要事先规定,不能在程序中进行调整,这样一来,在程序设计中针对不同问题有时需要3 0个大小的数组,有时需要5 0个数组的大小,难于统一。我们只能够根据可能的最大需求来定义数组,常常会造成一定存储空间的浪费。 我们希望构造动态的数组,随时可以调整数组的大小,以...

C++队列和链表

1、什么是队列【queue】 队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。 关于队列的基本操作方法: add():添加一个元素,若超出了度列的长度会直接抛出异常。 put():添加一个元素,若向队尾添加元素的时候发现队列已经满了会发生阻塞一直等待空间,以加入元素。 offer():添加一个元素,如果在添加时发现队列已满无...

运用c++语言打出数据结构中的静态链表代码,仅供参考【代码】

这个也算是较为简单的一种数据结构思想了,是古人们没有指针时候用的,虽然很古老,但是很实用,不如哈夫曼树的时候用这个就明显比指针好用的多,不废话,直接上代码。 里面有很多注释的内容是我用来测试代码的,不用管,但也可以将其放出来更好的理解代码。 #include using namespace std; #define MAXSIZE 1000 #define OK 1 #define Status int #define Elemtype char #define ERROR -1 static int length=0; typedef struct { ...

静态链表 C++版【代码】【图】

笔记静态链表的实现 1 #include "stdafx.h"2 #include<iostream>3 4 using namespace std;5 6 #define MAXSIZE 1007 8 typedef int ElemType;9 10 typedef struct { 11 ElemType data; 12 int cur; 13 }component,SLinkList[MAXSIZE]; 14 15 void InitSpace_SL(SLinkList &space) { 16 for (int i = 0; i < MAXSIZE - 1; ++i) space[i].cur = i + 1; 17 space[MAXSIZE - 1].cur = 0; 18 } 19 20 int Malloc_SL(...

LeetCode [链表]19.Remove Nth Node From End of List (C++和Python实现)

19.Remove Nth Node From End of List [难度:中等] 【题目】 Given a linked list, remove the n-th node from the end of list and return its head. Example:Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked list becomes 1->2->3->5.Note: Given n will always be valid. Follow up: Could you do this in one pass? 【解题C++】 (题外话:刚还想说用不惯LeetCode,感...

合并两个排序的链表 C++解法【代码】

题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。public ListNode Merge(ListNode list1, ListNode list2){//新建一个头节点,用来存合并的链表。ListNode head = new ListNode(-1);head.next = null;ListNode root = head;while (list1 != null && list2 != null){if (list1.val < list2.val){head.next = list1;head = list1;list1 = list1.next;}else{head.next = list...

递归,迭代,堆栈三种方式实现单链表反转(C++)【代码】

#include <stack> using namespace std;struct Node {int val;Node *next; };Node * creat_link() {Node *head=NULL;Node *current=NULL;Node *tmp=NULL;int val=0;int num_node=10;for(int i=0;i<num_node;i++){val=i+1;if(i==0){current=(Node*)new(Node);current->val=val;current->next=NULL;head=current;}else{tmp=(Node*)new(Node);tmp->val=val;tmp->next=NULL;current->next=tmp;current=current->next;}}return head; }v...

链表 - 相关标签