Cracking the coding interview--Q2.2

Ollama运行DeepSeek大模型调优指南:GPU与CPU协同实现性能最大化 Ollama运行DeepSeek大模型调优指南 阅读详情

原文:

Implement an algorithm to find the nth to last element of a singly linked list.

译文:

实现一个算法从一个单链表中返回倒数第n个元素。


package chapter_2_LinkedLists;

import java.util.Scanner;

/**
 * 实现一个算法从一个单链表中返回倒数第n个元素。
 */
public class Question_2_2 {
	
	/**
	 * 如果提前知道链表的长度,可以直接计算正向位置
	 */
	public static int findNthLast(LinkList linkList, int n) {
		Node p;
		p = linkList.head;
		if(n > linkList.size) {
			return -1;
		}		
		int locat = linkList.size - n + 1;
		int i = 1;
		boolean flag = true;
		while(flag) {
			if(i != locat) {
				i++;
				p = p.next;
			} else {
				flag = false;
			}
		}
		return p.data;
	}
	
	/**
	 * ********
	 * 如果提前不知道链表的长度,可以使用两个指针,间距保持n,只需要扫描一遍
	 */
	public static int findNthLast2(LinkList linkList, int n) { 
		Node p, q;
		p = linkList.head;
		int i = 1;
		q = p;
		while(i != n && q != null) {
			q = q.next;
			i ++;
		}
		if(q == null) {
			return -1;
		}
		// 上面步骤保持n-1,开始往后移动
		while(q.next != null) {
			q = q.next;
			p = p.next;
		}
		return p.data;
	}
	
	public static void main(String args[]) {
		
		Scanner scanner = new Scanner(System.in);
		LinkList linkList = new LinkList();
		int temp;
		int num = scanner.nextInt();
		for(int i=0; i< num; i++) {
			temp = scanner.nextInt();
			linkList.add(temp);
		}
		scanner.nextLine();
		int n = scanner.nextInt();
		
		int result = findNthLast2(linkList, n);
		if(result < 0) {
			System.out.format("error : %3d\n", result);
		} else {
			System.out.format("%3d\n", result);
		}
	}
}



android 蓝牙通讯demo android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。 立即下载

相关推荐

K8s认证文件丢失的应急处理与恢复策略

本文详细介绍了Kubernetes(K8s)认证文件丢失时的应急处理与恢复策略。从快速诊断问题范围到不同场景的临时解决方案,再到深度恢复方案(无损重建、部分重置、完全重建),提供了全面的操作指南。同时强调了生产环境中的防丢失设计、高可用集群的特殊处理以及日常运维建议,帮助运维人员有效应对认证文件丢失的紧急情况。

weixin_33743248的博客 326

Cracking the Coding Interview Q2.1

本博客记录我用C++刷cracking the coding interview的历程。。。

u011347440的专栏 721

6轴机械臂(拟人臂+球形腕(三轴相交于一点))正逆运动学求解

1、DH坐标下机械臂参数 theta=[pi/10,pi/2,pi/4,-pi/4,pi/4,-pi/8];%关节角度 a=[0, 0.260, 0.025, 0, 0, 0];%连杆长度 d=[0, 0, 0, 0.280, 0, 0.072];%偏移距离 alpha=[pi/2, 0, pi/2, -pi/2 , pi/2, 0];%RBR型6轴机械臂 使用...

qq_33374294的博客 7880

Cracking the coding interview--Q2.4

Cracking the coding interview--Q2.4 Write code to partition a linked list around a value x, such that all nodes less than xcome before alt nodes greater than or equal to. 分...

weixin_34388207的博客 109

【Docker】从零构建达梦DM8轻量级服务镜像:实战与优化

本文详细介绍了从零开始构建达梦DM8轻量级Docker镜像的完整实战流程。通过分析传统安装方式的痛点,阐述了使用Docker封装数据库的优势,并重点讲解了如何利用静默安装、精简组件、多阶段构建等优化技巧,打造一个体积小巧、启动迅速、配置灵活的DM8服务镜像,助力实现开发、测试与部署的环境一致性与高效性。

ooo22的博客 518

【硬件】通俗易懂的讲解晶体管(三极管和MOS管)的工作原理

晶体管是一个简单的元器件,可用于构建许多有趣的项目。在本文中,我将用通俗易懂的语言给您讲解晶体管的工作原理,以便您可以在电路设计中更好的使用静态管。一旦你学习这些基础知识,对以后的设计和使用来说,将会变得非常容易。我们将重点介绍两种最常见的晶体管:双极型晶体管(三极管)和MOSFET。晶体管的工作原理其实是类似于电子开关。它可以打开和关闭电路。一个简单的思考方法是将晶体管视为无源的继电器。晶体管类似于继电器,从某种意义上说,您可以使用它来打开和关闭某些东西。但晶体管也可以部分导通,一般在放大电路中使用,这部

1万+

Cracking the coding interview--Q4.2

原文: Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 译文: 给定一个有向图,设计算法判断两结点间是否存在路径。 根据题意,给定一个有向图和起点终点,判断从起点开始,是否存在一条路径可以到达终点。 考查的就是图

执子之手 395

Python+QMT量化交易实战:从零搭建你的第一个极速策略(附完整代码)

本文提供了一份详细的Python+QMT量化交易实战指南,手把手教你从零搭建一个可回测、可实盘的极速策略。内容涵盖环境配置、QMT核心机制解析、双均线策略的完整代码实现、回测优化方法以及实盘部署要点,帮助量化交易新手快速入门并掌握核心开发流程。

b9c0d的博客 1012

Cracking the coding interview--Q2

Chapter 2 | Linked Lists 2.1 Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? hash,否则只能o(n平方) 2.2

706

《Hadoop大数据技术原理与应用》(第2版)黑马程序员的课后习题答案

《Hadoop大数据技术原理与应用》(第2版)黑马程序员的课后习题答案

qq_36773158的博客 4646

分享通达信破解交易接口的使用!

那其实通达信破解交易接口的使用方法比较复杂,这里以MA均线指标为例子,只要理解了这些操作步骤,就可以很容易地使用其他指标包括自己编写的指标来进行交易。委托结果(包含委托编号), 需要分配 1024*1024 字节的空间。委托结果数组, 每项结果需要分配 1024*1024 字节的空间。0: 买入, 1: 卖出, 2: 融资买入, 3: 融券卖出,4: (市价委托)上海五档即成剩撤, 深圳五档即成剩撤。4: 买券还券, 5: 卖券还款, 6: 现券还券。0: 上海限价委托, 深圳限价委托。

1841085904的博客 2358

Cracking the Coding Interview Q2.2

题干如下:

u011347440的专栏 478

【3D无序抓取】--终极技术详解

3D无序抓取技术详解:从感知到执行的全流程技术方案 本文系统梳理了3D无序抓取(Bin Picking)的核心技术栈,涵盖3D感知、抓取规划和机器人执行三大环节。在3D感知层,详细介绍了点云采集与预处理方法(PCL/Open3D)、物体分割(PointNet++)和位姿估计(PVN3D/DenseFusion)等关键技术。抓取规划部分重点解析了抓取生成(GraspNet)、质量评估(Dex-Net)和动态避障(MoveIt!)算法。机器人执行环节则涉及路径规划、力控技术和末端执行器选型。同时提供了完整的工具

weixin_45590420的博客 596

Cracking the coding interview--Q2.3

题目 原文: Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node. EXAMPLE Input: the node ‘c’ from the linked list a->b->c->d->e Result: nothin

cfanr 1388

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Cracking the coding interview Q1.2

题目描述  Write code to reverse a C-Style String  (C-String means that “abcd” is represented as five characters, including the null character  ) 写一段代码对字符串进行翻转 char* reverseStr(char *str ) { if ( str

yuejiewc的专栏 869

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Cracking the coding interview--Q2.5

Cracking the coding interview--Q2.5 December 17, 2012 作者:Hawstein 出处:http://hawstein.com/posts/2.5.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处。 题目

pi9nc的专栏 817

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Cracking the coding interview--Q20.2

题目 原文:

cfanr 1525

Cracking the Coding Interview Q1.2

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_33923762的博客 379

Cracking the coding interview--Q8.2

题目 原文: Imagine a robot sitting on the upper left hand corner of an NxN grid. The robot can only move in two directions: right and down. How many possible paths are there for the robot? FOLLOW UP I

cfanr 1493

Cracking the coding interview--Q9.2

题目 原文: Write a method to sort an array of strings so that all the anagrams are next to each other. 译文: 写一个方法对一个字符串数组进行排序,并使所有变位词都相邻。 解答 首先需要理解什么是变位词?变位词就是字符组成的元素相同,只不过位置不同而已,例如:abc与bca是变位词;然后需

cfanr 1568

Cracking the Coding Interview Q2.5

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function...

weixin_30568715的博客 101

Cracking the coding interview--Q5.2

题目 原文: Given a (decimal - e.g. 3.72) number that is passed in as a string, print the binary representation. If the number can not be represented accurately in binary, print “ERROR” 译文: 给一个字符类型表示

cfanr 1341
上一篇: Cracking the coding interview--Q2.1
下一篇: Cracking the coding interview--Q2.3
java_wliang
博客等级 码龄15年 19粉丝 82原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值