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 第二章的更多相关文章

  1. Cracking the coding interview 第二章问题及解答

    文章的缘由可以参考此篇文章.目前完成了第二章,代码放在github上,地址在此.问题的描述都在对应的代码文件中.其他的章节仍在在进行中. 如果代码有问题,欢迎指正,谢谢. yetuweiba

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview 第一章

    第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...

  4. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  8. 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 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

随机推荐

  1. centos 4.4配置使用 and Nutch搜索引擎(第1期)_ Nutch简介及安装

    centos 4.4配置使用 1.Nutch简介 Nutch是一个由Java实现的,开放源代码(open-source)的web搜索引擎.主要用于收集网页数据,然后对其进行分析,建立索引,以提供相应的 ...

  2. jsoneditor显示Json data

    Git开源地址:https://github.com/josdejong/jsoneditor/blob/master/docs/api.md 1.引用JS文件 <!-- jsoneditor ...

  3. JqGrid帮助文档

    JQGrid是一个在jquery基础上做的一个表格控件,以ajax的方式和服务器端通信. JQGrid Demo 是一个在线的演示项目.在这里,可以知道jqgrid可以做什么事情. 下面是转自其他人b ...

  4. C#对文件的操作

    本文收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归删除文件夹及文件.指定文件夹下 面的所有内容copy到目标文件夹下面.指定文件夹下面的所有内 ...

  5. 10.26最后的模拟DAY2 改造二叉树[中序遍历+严格递增的最长不下降子序列]

    改造二叉树 [题目描述] 小Y在学树论时看到了有关二叉树的介绍:在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他 ...

  6. [转]How WebKit Loads a Web Page

    ref:https://www.webkit.org/blog/1188/how-webkit-loads-a-web-page/ Before WebKit can render a web pag ...

  7. .NET核心代码保护策略

    .NET核心代码保护策略-隐藏核心程序集 经过之前那个道德指责风波过后也有一段时间没写博客了,当然不是我心怀内疚才这么久不写,纯粹是程序员的通病..怎一个懒字了得,本来想写一些长篇大论反讽一下那些道德 ...

  8. 使用protobuf编写配置文件以及读写

    .proto文件示例 message Configure { required ; required uint32 port = ; } 写配置文件 Configure config; config. ...

  9. Web API CSRF保护实现

    Web API CSRF保护实现 这次自己实现了类似jQuery中ajax调用的方法,并且针对RESTFul进行了改造和集成,实现的A2D AJAX接口如下: $.ajax.RESTFulGetCol ...

  10. TOGAF架构内容框架之架构制品(下)

    TOGAF架构内容框架之架构制品(下) 4.2.31 数据生命周期图(Data Lifecycle Diagram) 数据生命周期图是在业务流程的约束之下对业务数据在其整个生命周期(从概念阶段到最终退 ...