Cracking the Coding Interview 第二章
2.2 链表中倒数第k个结点
输入一个链表,输出该链表中倒数第k个结点。
思路:快慢指针(error: 判断是否有可行解,否则返回null, while, if 后加空格)
/*
public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head, int k) {
if (head == null){
return null;
}
ListNode fast = head;
ListNode ans = head;
for (int i = 0; i < k; i++){
if (fast == null){
return null;
}
fast = fast.next;
}
while (fast != null){
fast = fast.next;
ans = ans.next;
}
return ans;
}
}
2.3 删除当前节点
实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点。
给定带删除的节点,请执行删除操作,若该节点为尾节点,返回false,否则返回true
思路:将当前的val转为下一个的val,释放下一个即可。
import java.util.*; /*
public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
}
}*/
public class Remove {
public boolean removeNode(ListNode pNode) {
// write code here
if (pNode == null || pNode.next == null){
return false;
}
//ListNode next = pNode.next;
pNode.val = pNode.next.val;
pNode.next = pNode.next.next;
return true;
}
}
2.4 链表分割(小于x的在一边。大于等于x的又在一边)
编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以后保持原来的数据顺序不变。
思路:(1)在原链表上找到第一个大的,将之后的小的往前移(error:移完之后不能立即向前,不然会错过连着的两个这种情况)
import java.util.*; /*
public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
}
}*/
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
if (pHead == null){
return null;
}
ListNode front = new ListNode(0);
front.next = pHead;
pHead = front;
while (pHead.next != null && pHead.next.val < x){
pHead = pHead.next;
}
if (pHead.next != null){
ListNode greater = pHead.next;
while (greater.next != null){
if (greater.next.val < x){
ListNode temp = greater.next;
greater.next = temp.next;
temp.next = pHead.next;
pHead.next = temp;
pHead = temp;
} else {
greater = greater.next;
}
}
}
pHead = front.next;
front = null;
return pHead;
}
}
(2)新建两个链表small large,指向对应元素即可。时间复杂度更大。
import java.util.*; /*
public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
}
}*/
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
/*if (pHead == null){
return null;
}
ListNode front = new ListNode(0);
front.next = pHead;
pHead = front;
while (pHead.next != null && pHead.next.val < x){
pHead = pHead.next;
}
if (pHead.next != null){
ListNode greater = pHead.next;
while (greater.next != null){
if (greater.next.val < x){
ListNode temp = greater.next;
greater.next = temp.next;
temp.next = pHead.next;
pHead.next = temp;
pHead = temp;
} else {
greater = greater.next;
}
}
}
pHead = front.next;
front = null;
return pHead;*/
ListNode small = new ListNode(0);
ListNode large = new ListNode(0);
ListNode tailSmall = small;
ListNode tailLarge = large;
while (pHead != null){
if (pHead.val < x){
tailSmall.next = new ListNode(pHead.val);
tailSmall = tailSmall.next;
} else {
tailLarge.next = new ListNode(pHead.val);
tailLarge = tailLarge.next;
}
pHead = pHead.next;
}
tailSmall.next = large.next;
return small.next;
}
}
Cracking the Coding Interview 第二章的更多相关文章
- Cracking the coding interview 第二章问题及解答
文章的缘由可以参考此篇文章.目前完成了第二章,代码放在github上,地址在此.问题的描述都在对应的代码文件中.其他的章节仍在在进行中. 如果代码有问题,欢迎指正,谢谢. yetuweiba
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview 第一章
第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
随机推荐
- Android 5.0自定义动画
材料设计中的动画对用户的操作给予了反馈,并且在与应用交互时提供了持续的可见性.材料主题提供了一些按钮动画和活动过渡,Android 5.0允许你自定义动画并且可以创建新的动画: Touch Feedb ...
- SpringMVC 国际化
SpringMVC学习系列(8) 之 国际化 在系列(7)中我们讲了数据的格式化显示,Spring在做格式化展示的时候已经做了国际化处理,那么如何将我们网站的其它内容(如菜单.标题等)做国际化处理呢? ...
- 关于ListView中convertView的缓存个数的探究
在面试的时候经常会被问到一个有关ListView的问题:一个ListView的高度最多可以显示5个item,但是却有20条数据要显示,问最多会有多少个convertView会被复用?或者如在ListV ...
- 关于使用 jBox 对话框的提交问题
http://www.cnblogs.com/haogj/archive/2012/11/04/2754303.html 关于使用 jBox 对话框的提交问题 jBox 是个不错的对话框组件. 在 A ...
- WCF Restful Service的服务
构建基于WCF Restful Service的服务 前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面 ...
- jsonp跨域+ashx
jsonp跨域+ashx(示例) 前言 做B/S项目的时候,我们一般使用jquery+ashx来实现异步的一些操作,比如后台获取一些数据到前台,但是如果ashx文件不在本项目下,引用的是别的域下的文件 ...
- IIS Express添加MIME映射
最近在使用fontawesome字体时,在浏览器控制台看到 fontawesome-webfont.woff2?v=4.3.0 无法访问的错误,检查了一下文件确实存在并且路径也对,这就奇怪了! 在控制 ...
- DevExpress 学习使用之 LookUpEdit
我的机器上,Winform 控件中的 LookUp 有1+3 种,在我们国家也被称为 4 种,你们那儿呢? 这 1+3 种 LookUpEdit 分别是 LookUpEdit.GridLookUpEd ...
- [转]ARM/Thumb/Thumb-2
ref:http://kmittal82.wordpress.com/2012/02/17/armthumbthumb-2/ A few months ago I gave a presentatio ...
- 迟到的 WPF 学习 —— 控件
这一章书中内容比较多而杂,但每个对象的内容又相对简短,所以只挑选里边有代表性的内容做记录. 1. Label 控件:一个基础的简单的 ContentControl,Labe 支持快捷键文本的设置,可以 ...