n个节点的二叉树n+1

Problem statement:

问题陈述:

Given a Linked List, we have to delete N numbers of nodes after the M numbers of nodes.

给定一个链表,我们必须在M个节点之后删除N个节点。

Example:

例:

    Input: 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10
N=2, M=3
Output: 1 → 2 → 3 → 6 → 7 → 8
Input: 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11
N=3, M=3
Output: 1 → 2 → 3 → 7 → 8 → 9

Algorithm:

算法:

To solve that problem we simply use the Brute-Force method. We traverse the linked list from the head node and delete N nodes after traversing M nodes. If there are less than N nodes to delete then we simply delete that nodes and stop traversing.

为了解决该问题,我们仅使用Brute-Force方法 。 我们从头节点遍历链表,并在遍历M个节点后删除N个节点 。 如果要删除的节点少于N个,则我们只需删除该节点并停止遍历即可。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
struct node {int data;
node* next;
};
//Create a new node
struct node* create_node(int x)
{struct node* temp = new node;
temp->data = x;
temp->next = NULL;
return temp;
}
//Enter the node into the linked list
void push(node** head, int x)
{struct node* store = create_node(x);
if (*head == NULL) {*head = store;
return;
}
struct node* temp = *head;
while (temp->next) {temp = temp->next;
}
temp->next = store;
}
//Reverse the linked list
void delete_node(node* head, int m, int n)
{struct node* temp = head;
int count = 1;
while (1) {while (temp && count < m) {temp = temp->next;
count++;
}
if (temp == NULL || temp->next == NULL) {return;
}
count = 1;
struct node* store = temp;
temp = temp->next;
while (temp && count < n) {temp = temp->next;
count++;
}
if (temp == NULL) {return;
}
store->next = temp->next;
temp = temp->next;
}
}
//Print the list
void print(node* head)
{struct node* temp = head;
while (temp) {cout << temp->data << " ";
temp = temp->next;
}
}
int main()
{struct node* l = NULL;
push(&l, 1);
push(&l, 2);
push(&l, 3);
push(&l, 4);
push(&l, 5);
push(&l, 6);
cout << "Before the delete operation" << endl;
print(l);
delete_node(l, 3, 2);
cout << "\nAfter the delete operation" << endl;
print(l);
return 0;
}

Output

输出量

Before the delete operation
1 2 3 4 5 6
After the delete operation
1 2 3 6

翻译自: https://www.includehelp.com/cpp-programs/delete-n-nodes-after-m-nodes-of-a-linked-list-using-cpp-program.aspx

n个节点的二叉树n+1

n个节点的二叉树n+1_使用C ++程序删除链接列表的M个节点后的N个节点相关推荐

  1. n个节点的二叉树n+1_使用C ++程序将链接列表中的最后N个节点附加到第一个

    n个节点的二叉树n+1 Given a linked list and an integer n, append the last n elements of the LL to front. Ass ...

  2. notepad++节点_在C ++中删除链接列表的中间节点

    notepad++节点 Given a single Linked List and we have to delete the middle the element of the Linked Li ...

  3. 微信小程序 查找兄弟节点_使用C ++程序在链接列表中查找节点

    微信小程序 查找兄弟节点 Given a linked list and an integer N, you need to find and return index where N is pres ...

  4. N个节点的二叉树的形态数详细推导

    0.总结 Get to the key point firstly, the article comes from LawsonAbs! N个节点的二叉树的形态数的推导 卡特兰数 相关例题的分析 1. ...

  5. 算法练习day10——190328(二叉树的先序、 中序、 后序遍历, 包括递归方式和非递归方式、找到一个节点的后继节点、二叉树的序列化和反序列化)

    1.实现二叉树的先序. 中序. 后序遍历, 包括递归方式和非递归方式 1.1 访问节点的顺序 节点访问顺序如下图所示: 访问顺序:1 2 4 4 4 2 5 5 5 2 1 3 6 6 6 3 7 7 ...

  6. 给定一个n节点的二叉树,写出一个O(n)时间非递归过程,将该树每个节点关键字输出,可以使用一个栈作为辅助数据结构(算法导论第十章10.4-3)

    给定一个n节点的二叉树,写出一个O(n)时间非递归过程,将该树每个节点关键字输出,可以使用一个栈作为辅助数据结构 (算法导论第十章10.4-3) template<typename T> ...

  7. 给定一个n节点的二叉树,写出一个O(n)时间递归过程,将该树每个节点关键字输出(算法导论第十章10.4-2)

    给定一个n节点的二叉树,写出一个O(n)时间递归过程,将该树每个节点关键字输出 (算法导论第十章10.4-2) #include <iostream> template<typena ...

  8. java根节点到叶子节点_二叉树根节点到叶子节点的所有路径和

    我觉得这个题目和剑指offer中的一道题目非常相似.先说这个题: 解题思路:从根结点开始,当每访问到一个结点,我们把该结点添加到路径上,并"累加" 该结点的值,这里"累加 ...

  9. 卡特兰数(n个节点的二叉树情况数量+hdu2067) (超级卡特兰数(施罗德数))

    卡特兰数是组合数学中一个常出现在各种计数问题中的数列. 举个栗子:1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742 ...

最新文章

  1. ML之xgboost:绘制xgboost的二叉树graphviz的两种方法代码实现
  2. Python学习笔记(5):Python如何忽略warning的输出
  3. ANDROID:SHOWASACTION="NEVER"是做什么用的?
  4. oracle 截取小数点_oracle函数(关于处理小数点位数和时间) | 学步园
  5. 红橙Darren视频笔记 ViewGroup事件分发分析 基于API27
  6. tablepc是什么平板电脑_tablepc平板电脑怎么截图
  7. 红帽Linux平台下安装Code::Blocks
  8. 面试字节跳动,我被怼了....
  9. 计算机链接与rtu通讯的区别,RTU
  10. 看大神如何玩转微信小程序日历插件?
  11. Java base64转inputStream
  12. bzoj 2959: 长跑 lct+并查集
  13. 深度学习:透过神经网络的内在灵魂与柏拉图的哲学理念
  14. python爬虫淘宝实例-python爬虫实例,一小时上手爬取淘宝评论(附代码)
  15. 计算机论文选题背景,毕业设计选题背景意义.docx
  16. Android8.1 修改音量级别和默认音量
  17. 如何知道您是否准备从Mac切换到Linux
  18. Ubuntu云服务器搭建饥荒联机版服务器教程
  19. Arcgis动态图制作——以全国各省情绪变化地图为例
  20. 3 WOW的各种文件的格式(WOWMapView Group)

热门文章

  1. json 对象 数组
  2. JS中的兼容问题总结
  3. CSS 小结笔记之em
  4. 前端图片canvas,file,blob,DataURL等格式转换
  5. css高度已知,左右定宽,中间自适应三栏布局
  6. html笔记(五)2D、3D、3D动画
  7. 实战Java内存泄漏问题分析 -- hazelcast2.0.3使用时内存泄漏 -- 2
  8. Oracle ——如何确定性能差的 SQL
  9. 拉拢中小网站 淘宝百度暗战升级...
  10. ASP.NET MEMBERSHIP的XML配置