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. 依赖注入和IOC

    http://www.bbsmvc.com/archiver/csharp/thread-831-1.html 本来想使用一下Ninject的,然后搜索了很久,都没找到比较详细的关于Ninject的使 ...

  2. android判断网络的类型

    转自:http://blog.csdn.net/xxxsz/article/details/8199031 判断网络类型是wifi,还是3G,还是2G网络 对不同的网络进行不同的处理,现将判断方法整理 ...

  3. 1关于script标签属性,注意点,浏览器文档模式,各种数据类型的转化

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. KTHREAD 线程调度 SDT TEB SEH shellcode中DLL模块机制动态

    KTHREAD 线程调度 SDT TEB SEH shellcode中DLL模块机制动态获取 <寒江独钓>内核学习笔记(5)   继续我们的线程相关的数据结构的学习.接下来我们学习 KTH ...

  5. Tomcat7 Cluster 集群

    Tomcat7 自带的集群功能是通过session复制完成的,现有两个复制方式: DeltaManager: 将session复制到所有tomcat节点中,不管是否有相应的应用(it will rep ...

  6. Day3:T1数论+高精 T2搜索

    T1:数论+高精(水~) 根据题意可知,从除的数越大越好(在0~9中) 所以我们只要用到高精除然后再模拟一下就可以了 //MARK:但是要注意0-9这个特殊值需要特判,因为题目要求输出的数至少是两位数 ...

  7. CentOS6.8安装JDK1.7

    一.查看当前系统是否自带JDK rpm -qa | grep java tzdata-java-2016c-1.el6.noarch java-1.7.0-openjdk-1.7.0.99-2.6.5 ...

  8. C#与Arduino通过串口通信来控制LED灯的状态

    一.引言 最近摆弄了一段时间的Arduino,发现Arduino做一些电子类项目.监控.机器人.电子玩具比较容易,并且Arduino与.NET程序集成也不难.接下来介绍一个简单的小程序,C#做的一个W ...

  9. JavaWEB开发国际化

    1.国际化开发概述 )软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. )国际化又称为 i18n:inter ...

  10. html5 “拖放”

    拖放主要是两个部分组成,drag:拖动,drop:放置!既抓取元素后拖到另一个位置! 要实现拖放首先要把被拖动元素设置为可拖动,既: draggbile="true" 然后要拖动什 ...