[Hackrank]Get Node Value

本文介绍了一种高效获取链表倒数第N个元素的方法,通过使用快慢两个指针,仅需遍历一次链表即可找到目标节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You’re given the pointer to the head node of a linked list and a specific position. Counting backwards from the tail node of the linked list, get the value of the node at the given position. A position of 0 corresponds to the tail, 1 corresponds to the node before the tail and so on.

Input Format 
You have to complete the int GetNode(Node* head, int positionFromTail) method which takes two arguments - the head of the linked list and the position of the node from the tail. positionFromTail will be at least 0 and less than the number of nodes in the list. You should NOT read any input from stdin/console.

Constraints 
Position will be a valid element in linked list.

Output Format 
Find the node at the given position counting backwards from the tail. Then return the data contained in this node. Do NOT print anything to stdout/console.

Sample Input

1 -> 3 -> 5 -> 6 -> NULL, positionFromTail = 0
1 -> 3 -> 5 -> 6 -> NULL, positionFromTail = 2

Sample Output

6

3

题意:其实这道题要求的就是linkedlist倒数位置的node的值,虽然说了一大推但其实实际没有变。

解题思路:linkedlist不能像数组那样直接取,所以我们要根据他的特点来设计解决方法,我们可以这样思考,既然要求倒数的值,那也就是说从头开始就要走n-k的步数。那么你当然可以先遍历一遍这个linkedlist,得到长度,再走到那个节点去算这个值。还有一种只需要走一遍的方法是,设定两个指针,都等于head,快的那个先走k步,走到k步后,两个指针一起走,等到快指针走到尾部的时候,正好走了n-k步,于是就相当于慢指针走了n-k步,所以slow停下来的节点就是我们需要的节点

/*
  Get Nth element from the end in a linked list of integers
  Number of elements in the list will always be greater than N.
  Node is defined as 
  class Node {
     int data;
     Node next;
  }
*/
    
int GetNode(Node head,int n) {
     // This is a "method-only" submission. 
     // You only need to complete this method.
    //快慢指针
    Node slow=head;
    Node fast=head;
   //快的先走
    for(int i=0;i<n;i++){
        fast=fast.next;
    }
    //一起走
    while(fast.next!=null){
        slow=slow.next;
        fast=fast.next;
    }
    return slow.data;

}


内容概要:本文探讨了在MATLAB/SimuLink环境中进行三相STATCOM(静态同步补偿器)无功补偿的技术方法及其仿真过程。首先介绍了STATCOM作为无功功率补偿装置的工作原理,即通过调节交流电压的幅值和相位来实现对无功功率的有效管理。接着详细描述了在MATLAB/SimuLink平台下构建三相STATCOM仿真模型的具体步骤,包括创建新模型、添加电源和负载、搭建主电路、加入控制模块以及完成整个电路的连接。然后阐述了如何通过对STATCOM输出电压和电流的精确调控达到无功补偿的目的,并展示了具体的仿真结果分析方法,如读取仿真数据、提取关键参数、绘制无功功率变化曲线等。最后指出,这种技术可以显著提升电力系统的稳定性与电能质量,展望了STATCOM在未来的发展潜力。 适合人群:电气工程专业学生、从事电力系统相关工作的技术人员、希望深入了解无功补偿技术的研究人员。 使用场景及目标:适用于想要掌握MATLAB/SimuLink软件操作技能的人群,特别是那些专注于电力电子领域的从业者;旨在帮助他们学会建立复杂的电力系统仿真模型,以便更好地理解STATCOM的工作机制,进而优化实际项目中的无功补偿方案。 其他说明:文中提供的实例代码可以帮助读者直观地了解如何从零开始构建一个完整的三相STATCOM仿真环境,并通过图形化的方式展示无功补偿的效果,便于进一步的学习与研究。
编写一个表示链表节点的模板类node,实现链表节点的以下功能: set_value函数:设置节点值 get_value函数:获取节点值 get_prev函数:获取上一个节点 get_next函数:获取下一个节点 insert函数:在当前节点的位置插入新节点(当前节点后移) 例如,下列程序中, test1函数的输出应为:1 2 3 test2函数的输出应为:1.1 2.2 3.3 test3函数的输出应为:a b c 部分代码已给出,请将代码补充完整。#include <iostream> using namespace std; void test1() { node<int> *ptr; node<int> node1,node2,node3; node1.set_value(1); node2.set_value(2); node3.set_value(3); node3.insert(&node2); node2.insert(&node1); for(ptr=&node1 ; ; ptr=ptr->get_next()) { cout << ptr->get_value() << " "; if(ptr->get_next()==NULL) break; } } void test2() { node<float> *ptr; node<float> node1,node2,node3; node1.set_value(1.1); node2.set_value(2.2); node3.set_value(3.3); node3.insert(&node2); node2.insert(&node1); for(ptr=&node1 ; ; ptr=ptr->get_next()) { cout << ptr->get_value() << " "; if(ptr->get_next()==NULL) break; } } void test3() { node<char> *ptr; node<char> node1,node2,node3; node1.set_value('a'); node2.set_value('b'); node3.set_value('c'); node3.insert(&node2); node2.insert(&node1); for(ptr=&node1 ; ; ptr=ptr->get_next()) { cout << ptr->get_value() << " "; if(ptr->get_next()==NULL) break; } } int main( ) { int type; cin >> type; switch(type) { case 1: test1(); break; case 2: test2(); break; case 3: test3(); break; } return 0; }
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值