notepad++节点

Given a single Linked List and we have to delete the middle the element of the Linked List.

给定一个链表,我们必须删除链表中间的元素。

If the length of the linked list is odd then delete (( n+1)/2)th term of the linked list and if the list is of even length then delete the (n/2+1)th term of the liked list.

如果链接列表的长度为奇数,则删除链接列表的第((n + 1)/ 2)项;如果列表的长度为偶数,则删除喜欢列表的第(n / 2 + 1)项。

Example 1:

范例1:

    If we have a Linked List : 1 → 2 → 3 → 4 → 5 → 6 → 7
After deleting the middle node the linked list will be:
1 → 2 → 3 → 5 → 6 → 7
4 is the middle node

Example 2:

范例2:

    If we have a Linked List : 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8
After deleting the middle node the linked list will be:
1 → 2 → 3 → 4 → 6 → 7 → 8
5 is the middle node

Algorithm:

算法:

To solve the problem we follow the following procedure,

为了解决该问题,我们遵循以下过程,

  1. We initiate the two node pointer name as slow, fast. (like Floyd's tortoise algo, refer the link to my article of merge sort in the linked list).

    我们将两个节点的指针名称初始化为慢,快。 (就像弗洛伊德(Floyd)的乌龟算法一样,请在链接列表中引用我的合并排序文章的链接)。

  2. Each time we increment the slow by one whereas increment the fast pointer by two.

    每次我们将慢速指针加一,而快速指针加二。

  3. Repeat step 2 until the fast pointer goes to the end of the linked list.

    重复步骤2,直到快速指针移到链接列表的末尾。

  4. When fast pointer goes to the end of the list at the same time slow pointer points to the middle of the linked list.

    当快速指针同时到达列表末尾时,慢速指针指向链接列表的中间。

  5. Then delete the middle node.

    然后删除中间节点。

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;
}
//Delete the middle node from the linked list
void delete_node(node** head){if((*head)->next==NULL){*head=NULL;
return;
}
struct node* fast=(*head)->next;
struct node* slow=*head;
while(fast && fast->next && fast->next->next){slow=slow->next;
fast=fast->next->next;
}
slow->next=slow->next->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);
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 5 6

翻译自: https://www.includehelp.com/cpp-programs/delete-the-middle-node-of-a-linked-list-in-cpp.aspx

notepad++节点

notepad++节点_在C ++中删除链接列表的中间节点相关推荐

  1. vc++ 6.0 堆栈_在C ++中使用链接列表实现堆栈

    vc++ 6.0 堆栈 To implement a stack using a linked list, basically we need to implement the push() and ...

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

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

  3. bst 删除节点_在BST中删除大于或等于k的节点

    bst 删除节点 Problem statement: 问题陈述: Given a BST and a value x, write a function to delete the nodes ha ...

  4. python去除字符串中的单词_从字符串中删除单词列表

    看着你问题的其他答案,我注意到他们告诉你如何做你想做的事,但他们没有回答你在最后提出的问题.If the input query is "What is Hello", I get ...

  5. C语言删除单链列表中间的节点的算法(附完整源码)

    C语言删除单链列表中间的节点的算法 C语言删除单链列表中间的节点的算法完整源码(定义,实现,main函数测试) C语言删除单链列表中间的节点的算法完整源码(定义,实现,main函数测试) #inclu ...

  6. 《程序员代码面试指南》第二章 链表问题 在单链表和双链表中删除倒数第K个节点...

    题目 在单链表和双链表中删除倒数第K个节点 java代码 /*** @Description:在单链表和双链表中删除倒数第K个节点* @Author: lizhouwei* @CreateDate: ...

  7. python链表中删除一个节点数据_python实现单链表中删除倒数第K个节点的方法

    本文实例为大家分享了python实现单链表中删除倒数第K个节点的具体代码,供大家参考,具体内容如下 题目: 给定一个链表,删除其中倒数第k个节点. 代码: class LinkedListAlgori ...

  8. c语言单链表删除倒数第k个数,在单链表中删除倒数第k个节点

    实现方式很多,在这里只说两种实现方式.看不懂时候,大家可以画画图,对理解为什么很有帮助. 第一种方式: 1.首先判断K值和链表是否为空,如果k<=0,或链表为空,直接返回head: 2.满足上面 ...

  9. java字符串删掉子串_如何从Java中的列表中删除子列表?

    java字符串删掉子串 从列表中删除子列表 (Removing SubList from a List) Suppose, we have a list of few elements like th ...

最新文章

  1. JS 对象机制深剖——new 运算符
  2. mysql event type_MySQL 【Row】下的 Event_type
  3. c语言蚂蚁搬,《蚂蚁搬豆》教案
  4. 中值滤波medianBlur
  5. center.php,mycenter.php
  6. Percona XtraBackup User Manual 阅读笔记
  7. servlet实现新闻控制
  8. 开发技术理论学习与实践的关系
  9. markdown一边写一边预览_MarkDown使用笔记
  10. XStream 初探,很给力
  11. linux查看内存参数
  12. 【Python脚本进阶】2.4、conficker蠕虫(中):Python脚本与Metasploit交互
  13. Win7旗舰版无法安装ie10和ie11(提示:操作系统不受支持)
  14. Frontend Development
  15. 老将引退AMD换血成功
  16. IMX6ULL设备上面调试SDIOwifi。
  17. template报下面的错误
  18. 论文阅读笔记markdown模板
  19. SpringBoot实现人脸识别功能,亲测可用!
  20. 2FA的完整形式是什么?

热门文章

  1. java memcmp_memcmp,memicmp函数
  2. python socket udp并发_Python进阶----UDP协议使用socket通信,socketserver模块实现并发
  3. line-height 属性
  4. JS里面的懒加载(lazyload)
  5. 响应式方案调研及前端开发管理思考
  6. margin赋值为负值的几种效果(负值像素,负值百分数)
  7. Chrome调试WebView时Inspect出现空白的解决方法(使用离线包不Fan墙)
  8. iOS开发 之 可穿戴设备 蓝牙4.0 BLE 开发
  9. Android特效 五种Toast详解
  10. 2013年5月7日---JS中的正则