1007. Find the middle element in a Linked List

097634e9169c6777120550ad61afb6bc-1.jpg 立即下载
Description

Assuming the following definition:

template <typename Node_entry>struct Node {
   Node_entry entry;
   Node *next;
   Node();
   Node(Node_entry item, Node *add_on = NULL);
};

template <typename Node_entry> Node<Node_entry>::Node()
{
   next = NULL;
}

template <typename Node_entry> Node<Node_entry>::Node(Node_entry item, Node *add_on)
{
   entry = item;
   next = add_on;
}

Implement the following function:
template<typename T> const Node<T>* medianElement( const Node<T> *  head )

//Precondition: the linked list head is not empty.(但是需要判断为头空时的情况!)
//Postcondition: it returns the pointer to the middle element in the linked list head.
//If there are even number of elements in the list, the first one of the two middle elements is returned. 
 

For example,



 


在单链表中找到中间的元素。
#include <iostream>
using namespace std;

template <typename Node_entry>struct Node {
    Node_entry entry;
    Node *next;
    Node();
    Node(Node_entry item, Node *add_on = NULL);
};

template <typename Node_entry> Node<Node_entry>::Node()
{
    next = NULL;
}

template <typename Node_entry> Node<Node_entry>::Node(Node_entry item, Node *add_on)
{
    entry = item;
    next = add_on;
}

template<typename T> const Node<T>* medianElement( const Node<T> *  head ) {
    int count = 0;
    if (head == NULL) {
        return head;
    }
    const Node<T>* temp = new Node<T>();
    temp = head;
    
    //计算链表的长度
    while (temp != NULL) {
        count++;
        temp = temp->next;
    }
    
    //计算出中位数
    int half = 1;
    if (count % 2 == 0) {
        half = count / 2;
    }
    else half = count / 2 + 1;
    
    //获得元素
    while (half != 1) {
        head = head->next;
        half--;
    }
    return head;
}


dht11在oled上显示温度 stm32l432kc,如何实现? 🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。 阅读详情

相关推荐

1.2 重叠因子:双重指数移动平均(Double Exponential Moving Average, DEMA)概念与Python实战

双重指数移动平均(Double Exponential Moving Average, DEMA)是一种改进的移动平均线指标,由Patrick Mulloy在1994年开发。相比传统的指数移动平均线(EMA),DEMA通过减少滞后性来提供更快的市场反应。它通过两次计算EMA并进行特殊组合来实现这一目标,可以在保持平滑性的同时减少滞后。

Kewin的博客 2169

How to find middle element of LinkedList in Java in one pass

How do you find middle element of LinkedList in one pass is a programming question often asked to Java and non Java programmers in telephonic Interview. This question is similar to checking palind

Schen6 Online Life 1409

宏杉MacroSan存储开局指导-流程

目录 1. 导入ODSP授权 2.1.版本确认 2.2.存储设备升级U盘、升级光盘制作方法 开局流程如下: 1. 导入ODSP授权 ODSP授权文件随设备附带,拆箱可见 2.1.版本确认 java模式 html模式 2.2.存储设备升级U盘、升级光盘制作方法 准备工具 空白U盘: 1个,容量要求大于等于4GB。仅制作升级U盘时需要。 制作升级U盘步骤 1.将U盘插入PC机USB口,保证PC机能识别到U盘。 2.运行UltraISO,选择菜单中的 文件-&g...

飞天板砖 4280

[Haskell] CodeWars|Find the middle element

https://www.codewars.com/kata/545a4c5a61aa4c6916000755/train/haskell题目翻译本题中你需要写个函数,输入一个三元组,输出三个树中既不是最大的也不是最小的那个数字的下标。样例gimme (2, 3, 1) = 0 gimme (5, 10, 14) = 1题解只使用maximum和minimum的方法,这是我写的一个比较奇怪的方法。。g

とある黄鱼のOIブログ 326

传统(经典)粒子群算法(PSO)解决单目标优化问题

智能算法:粒子群算法(particle swarm optimizer)—PSO %原理:(1)种群初始化–N(种群数)–D(个体数)–x(i)(粒子位置)–v(i)(粒子速度) % (2)计算每个粒子的适应度值–而适应度值需要选择适应度函数 % (3)对于每个粒子,使用它的适应度值fit[i]和个体的极值pbest(i)进行比较,如果fit[i]>pbest(i),则pbest(i)=fit[i] % (4)同理,对每个粒子用它的适应值与全局极值进行比较,如果比它大,则

weixin_51631044的博客 1295

一个代码循环计算水在石墨烯表面的接触角

石墨烯接触角

Rockyoung 2578

用O(1)的时间复杂度删除单链表中的某个节点

给定链表的头指针和一个结点指针,在O(1)时间删除该结点。链表结点的定义如下: struct ListNode { int m_nKey; ListNode* m_pNext; }; 函数的声明如下: void DeleteNode(ListNode* pListHead, ListNode* pToBeDeleted); 这...

weixin_34194087的博客 340

【时时三省】tessy 集成测试:小白入门指导手册

有图有文字详细介绍 tessy 集成测试 操作方法。

时时三省的博客 2517

LintCode-228: Middle of Linked List (链表找中点)

解法1:快慢指针。快指针每次走2步,慢指针每次一步。快指针到末尾时,慢指针即指向重点。 /** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this-&amp;gt;val = va...

roufoo的博客 825

Hermes 教程 05:多 Agent 编排

本文介绍了Hermes v0.13.0中的四种多Agent协作模式:1)delegate_task快速子任务模式,适合2-3个独立并行任务;2)spawn子Agent模式,实现零成本并行多步流水线;3)tmux交互子Agent模式,支持长期自主任务;4)全新Kanban看板系统,提供持久化AI团队协作框架。各模式在隔离级别、时长、工具访问和适用场景上存在差异,其中spawn模式通过独立上下文窗口实现零上下文成本。文章详细说明了每种模式的使用方法、限制条件和典型应用场景,并提供了具体代码示例和可视化流程图。K

程序猿_三产 537

爬取B站评论+AI情感分析:Python批量抓数据+BERT正负向分类(全流程)

B站作为年轻群体聚集的内容平台,评论区蕴含海量用户真实态度(如好评、吐槽、中立观点),是情感分析的优质数据源!本文分享完整流程:批量爬取B站视频评论→数据清洗去噪→BERT模型情感分类→可视化分析,基于预训练BERT模型实现高精度正负向判断,新手也能快速复刻情感分析系统! 前置配置(关键!获取B站API访问权限) B站评论通过官方API接口返回,无需复杂解析,只需配置用户Cookie即可正常请求: 2. 完整代码(直接复制运行) 三、使用步骤(5步搞定,新手友好) 步骤1:配置关键参数 打开,在类中修改

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1069

Middle of the Linked List

Find the middle node of a linked list. Example Example 1: Input: 1->2->3 Output: 2 Explanation: return the value of the middle node. Example 2: Input: 1->2 Output: 1 Explanation: ...

leetcode 解题思路 456

【原创】下载RealEstate10K数据集原始视频的方法

目前互联网上能搜到下载RealEstate10K数据集原始视频的方法都已经不能用了,这篇博客介绍一种目前可用的下载RealEstate10K数据集原始视频的方法,并给出自动化的脚本代码。

沉迷单车的追风少年 2463

【LeetCode】Middle of Linked List 查找链表中点 - Easy

Middle of Linked List Find the middle node of a linked list.Example Given 1->2->3, return the node with value 2. Given 1->2, return the node with value 1.法①(Java)快慢指针 /** * Definition for ListNode

It’s All Uphill From Here 2545

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

单链表算法面试题

question: find the middle element in one traversal answer: use two pointer with one step and two steps, when the two steps pointer ends, the location where one step pointer runs is the answer ques

花果山 1238

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

LeetCode-Middle of the Linked List

LeetCode Java Middle of the Linked List

坟头草一米七五 572

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【LeetCode】876. Middle of the Linked List 解题报告(Python)

【LeetCode】876. Middle of the Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/middle...

负雪明烛 2988

1001. Bacon Number

Description The Bacon number of an actor is the number of degrees of separation he or she has from Bacon. For example, Tom Hanks has a Bacon number 1, because he was in Apollo 13 with Kevin Bacon.

sss_coder的专栏 798

1003. Width of Binary Trees

Description The maximum number of nodes in all levels in a binary tree is called the width of the tree. For example, the binary tree on the right in the following figure has width 4 because the max

sss_coder的专栏 706

1005. 业绩排名

Description 年末公司将职员按照业绩从大到小排序后分配奖金。假定输入是职员姓名以及业绩, 业绩是正整数。 请你按照职员业绩从高到低将职员排序,业绩相同时按照输入先后次序排序。 输出是排序后的名次和职员姓名列表, 业绩相同时,名次相同。 例如,输入: (wang ming, 320), (gao min, 330), (zhang li, 280), (luo yan, 320

sss_coder的专栏 644

1010. bracket matching

Description Implement the following function that decides if a list of brackets match. bool brackets(string s); /* The input s is a list of brackets, including {,},(,),[,], and possibly blanks.

sss_coder的专栏 630

1011. Find the external path length

Description Given the linked representation of binary trees typedef int T; struct BinaryNode{ T data; //store data  BinaryNode *left, *right; BinaryNode(T d, BinaryNode *l=NULL, BinaryNode*

sss_coder的专栏 626

1004. Hashing

Description Implement the following Hash class using separate chaining. template class HashTable {   public:      HashTable(int (* h)(HashedObj x), int size = 101 )       : currentSize( 0

sss_coder的专栏 570

1000. Minimum Weight

Description Given a list of weights w1, w2, ..., wn, we can construct a binary tree with n leaves, where each leaf is marked with one weight. The is called a weighted binary tree. The weight of t

sss_coder的专栏 528

1002. Is It Acyclic

Description Given a directed graph, decide if it is acyclic (there is no directed cycle). Input There are multiple test cases. The first line contains an integer T, indicating the number of

sss_coder的专栏 400

1006. Join two lists

Description list join(const  list &a, const list &b) //returns a list of distinct integers both in a and b. For example, a = (3,4,3,5); b = (2,3,2,12,4,4), then c = (3,4). or c = (4,3).  The ord

sss_coder的专栏 361
上一篇: 1006. Join two lists
下一篇: 1010. bracket matching
Eden_LZH
博客等级 码龄12年 1粉丝 13原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值