面试题2.1:编写代码,移除未排序链表中的重复结点  进阶:如果不得使用临时缓冲区,该怎么解决?

package cc150;

import java.util.HashMap;
import java.util.Map; public class DeleteDups { public static void main(String[] args) {
// TODO 自动生成的方法存根
LinkedListNode Node0 = new LinkedListNode(1);
LinkedListNode Node1 = new LinkedListNode(1);
LinkedListNode Node2 = new LinkedListNode(2);
LinkedListNode Node3 = new LinkedListNode(3);
Node0.next = Node1;
Node1.next = Node2;
Node2.next = Node3;
deleteDups(Node0);
LinkedListNode temp = Node0;
while(temp != null){
System.out.println(temp.iData);
temp = temp.next;
}
} // public static void deleteDups(LinkedListNode n){
// Map<Integer,Boolean> map = new HashMap<Integer,Boolean>();
// LinkedListNode pre = null; //记录上一次不重复的结点
// while(n != null){
// if(map.containsKey(n.iData)){
// pre.next = n.next;
// }else{
// map.put(n.iData, true);
// pre = n;
// }
// n = n.next;
// }
//
// } public static void deleteDups(LinkedListNode head){
if(head == null)
return;
LinkedListNode current = head;
while(current != null){
LinkedListNode runner = current;
while(runner.next != null){ //是runner.next
if(runner.next.iData == current.iData) //判断内层所有结点是否等于外层结点
runner.next = runner.next.next;
else
runner = runner.next;
}
current = current.next;
}
} }

面试题2.2:实现一个算法,找出单向链表中倒数第k个结点。——《剑指Offer》面试题15 (找出) &《Leetcode》removeNthNode (移除)

package cc150;

public class NthToLast {

	public static void main(String[] args) {
// TODO 自动生成的方法存根
ListNode Node1 = new ListNode(1);
ListNode Node2 = new ListNode(2);
ListNode Node3 = new ListNode(3);
Node1.next = Node2;
Node2.next = Node3;
System.out.println(nthToLast(Node1,1).val);
} public static ListNode nthToLast(ListNode head,int k){ //返回链表的倒数第k个结点
if(k <= 0)
return null;
ListNode p1 = head;
ListNode p2 = head; for(int i=0;i<k-1;i++){
if(p2 == null)
return null;
p2 = p2.next;
}
if(p2 == null)
return null; while(p2.next != null){
p1 = p1.next;
p2 = p2.next;
}
return p1;
} public static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} }

面试题2.3:实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点。

  思路:因为是单向链表,所以不知道一个结点的前一个结点,所以当这个结点是最后一个的时候,无解

     只要把要删除的结点的下一个结点的数据复制到这个结点即可。

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 nextNode = pNode.next;//取得要删除结点的下一个结点
pNode.val = nextNode.val;
pNode.next = nextNode.next;
return true;
}
}

 面试题2.4:编写代码,以给定值x为基准讲链表分割成两部分,所有小于x的结点排在大于或者等于x的结点之前。

package cc150;

public class Partition {

	public static void main(String[] args) {
// TODO 自动生成的方法存根
ListNode Node1 = new ListNode(5);
ListNode Node2 = new ListNode(4);
ListNode Node3 = new ListNode(3);
ListNode Node4 = new ListNode(2);
ListNode Node5 = new ListNode(1);
Node1.next = Node2;
Node2.next = Node3;
Node3.next = Node4;
Node4.next = Node5;
ListNode temp = partition(Node1,3); while(temp != null){
System.out.println(temp.val);
temp = temp.next;
}
} public static ListNode partition(ListNode pHead, int x) {
// write code here
ListNode beforeStart = null; //记录链表的头结点
ListNode afterStart = null; //记录链表的头结点
ListNode beforeEnd = null;
ListNode afterEnd = null; ListNode pNext = pHead;
while(pNext != null){ if(pNext.val < x){
if(beforeStart == null){
beforeStart = pNext;
beforeEnd = pNext;
}else{
beforeEnd.next = pNext;
beforeEnd = beforeEnd.next;
}
}else{
if(afterStart == null){
afterStart = pNext;
afterEnd = pNext;
}else{
afterEnd.next = pNext;
afterEnd = afterEnd.next;
}
}
pNext = pNext.next;
} //切记断掉after最后一个元素的next,不然会形成环
if(afterEnd != null)
afterEnd.next = null; //如果beforeStart为null,返回afterStart头结点
if(beforeStart == null)
return afterStart; beforeEnd.next = afterStart;
return beforeStart;
} public static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} }

 面试题2.5:给定两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并用链表形式返回结果。

  进阶:假设这些数位是正向存放的,请再做一遍。

package cc150;

import cc150.Partition.ListNode;

public class Plus {

	public static void main(String[] args) {
// TODO 自动生成的方法存根
ListNode Node1 = new ListNode(7);
ListNode Node2 = new ListNode(2);
ListNode Node3 = new ListNode(3);
Node1.next = Node2;
Node2.next = Node3; ListNode Node4 = new ListNode(4);
ListNode Node5 = new ListNode(5);
ListNode Node6 = new ListNode(6);
ListNode Node7 = new ListNode(7);
Node4.next = Node5;
Node5.next = Node6;
Node6.next = Node7; ListNode temp = plusAB(Node1,Node4);
System.out.println(temp.val);
while(temp !=null){
System.out.print(temp.val);
temp = temp.next;
}
} public static ListNode plusAB(ListNode a, ListNode b) {
// write code here
if(a == null && b == null)
return null;
int carry = 0;
ListNode result_temp = new ListNode(0);
ListNode result = result_temp;
while(a != null && b != null){
int value = a.val + b.val + carry; if(value>=10){
result_temp.next = new ListNode(value-10);
carry = 1;
}else{
result_temp.next = new ListNode(value);
carry = 0;
}
result_temp = result_temp.next;
a = a.next;
b = b.next;
}
if(a != null){
result_temp.next = new ListNode(a.val+carry);
result_temp.next.next = a.next;
}
else if(b != null){
result_temp.next = new ListNode(b.val+carry);
result_temp.next.next = b.next;
}
else if(carry == 1){
result_temp.next = new ListNode(carry);
} return result.next;
} public static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} }

面试题2.6:给定一个有环链表,实现一个算法返回环路的开头结点。

package cc150;

import cc150.Plus.ListNode;

public class FindBeginning {

	public static void main(String[] args) {
// TODO 自动生成的方法存根
ListNode Node1 = new ListNode(1);
ListNode Node2 = new ListNode(2);
ListNode Node3 = new ListNode(3);
ListNode Node4 = new ListNode(4);
ListNode Node5 = new ListNode(5);
ListNode Node6 = new ListNode(6);
ListNode Node7 = new ListNode(7);
ListNode Node8 = new ListNode(8);
ListNode Node9 = new ListNode(9); Node1.next = Node2;
Node2.next = Node3;
Node3.next = Node4;
Node4.next = Node5;
Node5.next = Node6;
Node6.next = Node7;
Node7.next = Node8;
Node8.next = Node9;
Node9.next = Node4; System.out.println(findBeginning(Node1).val);
} public static ListNode findBeginning(ListNode head){
ListNode slow = head;
ListNode fast = head;
//slow移动一步,fast移动两步,链表到头或者碰撞的时候停止
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
break;
}
//如果是链表到头,没有环路
if(fast == null || fast.next == null)
return null;
//将slow指向链表头部,fast指向碰撞处,两者同时移动,必回在环路起点相遇
slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return fast;
} public static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} }

面试题2.7: 编写一个函数,检查链表是否为回文。

  CC150第一种解法:反转并比较

import java.util.*;

/*
public class ListNode {
int val;
ListNode next = null; ListNode(int val) {
this.val = val;
}
}*/
public class Palindrome {
public boolean isPalindrome(ListNode pHead) {//反转并比较,CC150第一种解法
// write code here
if(pHead == null || pHead.next == null)
return true;
ListNode slow,fast; //快慢指针法查找链表的中心
slow = fast = pHead;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
if(fast != null){ //链表个数奇数个
slow.next = reverseList(slow.next);
slow = slow.next; //去掉中间的数
}else{
slow = reverseList(slow);
}
while(slow != null){
if(pHead.val != slow.val)
return false;
slow = slow.next;
pHead = pHead.next;
}
return true;
} public ListNode reverseList(ListNode head) {
if(head ==null){
return null;
}
Stack<ListNode> stack = new Stack<ListNode>();
ListNode current = head;
while(current != null){
stack.add(current);
current = current.next;
}
head = stack.pop();
current = head;
while(stack.empty() != true){
current.next = stack.pop();
current = current.next;
}
current.next = null; //Memory Limit Exceeded
return head;
}
}

面试题目——《CC150》链表的更多相关文章

  1. 面试大总结:Java搞定面试中的链表题目总结

    package LinkedListSummary; import java.util.HashMap; import java.util.Stack; /** * http://blog.csdn. ...

  2. (转)面试大总结之一:Java搞定面试中的链表题目

    面试大总结之一:Java搞定面试中的链表题目 分类: Algorithm Interview2013-11-16 05:53 11628人阅读 评论(40) 收藏 举报 链表是面试中常出现的一类题目, ...

  3. C/C++ 笔试、面试题目大汇总 转

    C/C++ 笔试.面试题目大汇总 这些东西有点烦,有点无聊.如果要去C++面试就看看吧.几年前网上搜索的.刚才看到,就整理一下,里面有些被我改了,感觉之前说的不对或不完善. 1.求下面函数的返回值( ...

  4. 前端面试题目汇总摘录(JS 基础篇)

    JS 基础 JavaScript 的 typeof 返回那些数据类型 object number function boolean undefined string typeof null; // o ...

  5. (转)喜马拉雅2018 Java面试题目

    背景:将网上的题目整理下. java基础 1:hashTable hashMap ConcurrentHashMap 的区别.数据结构.线程安全 2:equals和==区别, 重写equals一定要重 ...

  6. C++程序员面试题目总结(涉及C++基础、多线程多进程、网络编程、数据结构与算法)

     说明:C++程序员面试题目总结(涉及C++基础知识.多线程多进程.TCP/IP网络编程.Linux操作.数据结构与算法) 内容来自作者看过的帖子或者看过的文章,个人整理自互联网,如有侵权,请联系作者 ...

  7. C语言经典面试题目(转的,不过写的的确好!)

    第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量在这一 ...

  8. linux面试题目—2

    linux面试题目—2 二 选择题 1.关闭linux系统(不重新启动)可使用命令 B . A Ctrl+Alt+Del B halt C shutdown -r now D reboot 2.实现从 ...

  9. linux面试题目--1

    Linux面试题目 填空题1. 在Linux系统中,以 (文件)方式访问设备 .2. Linux内核引导时,从文件/etc/fstab 中读取要加载的文件系统.3. Linux文件系统中每个文件用i节 ...

  10. vc面试题目

    class B { public: B() { cout << "default constructor" << endl; } ~B() { cout & ...

随机推荐

  1. 推荐一个不错的css3网站 可以直接调用的

    animate.css 一搜就能出来  我用着还不错

  2. [iOS]坑爹的ALAsset(Assets Library Framework)

    Assets Library Framework 可以用来做iOS上的多选器,选照片视频啥的啦就不介绍了. 目前的项目有点类似dropbox,可以选择设备内的照片然后帮你上传文件,使用了Assets ...

  3. 使用Windbg在XP下Heap追踪失败的原因

    1.故事背景      最近同事的代码中碰到一个bug会导致奔溃的bug,从dump上看是由于某个对象的堆内存指针被释放了,但代码仍调用了该对象指针的虚函数,从而引起内存访问违法崩溃,由于该类被大量使 ...

  4. C#创建委托实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyDe ...

  5. maven核心,pom.xml详解(转)

    什么是pom?    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的u ...

  6. Excellent Articles

    Lisp The roots of lisp Recursive Functions of Symbolic Expressions and Their Computation by Machine, ...

  7. 有关sql server 2008无法导入数据库mdf文件的处理方法

    解决方法1:根据该博客中的引导,加上自己安装版本的细节,可以添加成功 http://www.2cto.com/database/201408/328930.html 解决方法2: 根据<数据库系 ...

  8. YII2如何修改默认控制器/方法

    在网上找了非常多的方法,但是都不好使最后自己综合网上所有自己琢磨出来的,见笑了 首先Yii2中在/vendor/yiisoft/yii2/web/Application.php的第28行 public ...

  9. C#基础系列——反射笔记

    前言:使用反射也有几年了,但是一直觉得,反这个概念很抽象,今天有时间就来总结下这个知识点. 1.为什么需要反射: 最初使用反射的时候,作为小菜总是不理解,既然可以通过new 一个对象的方式得到对象,然 ...

  10. 你可能不知道的 NaN 以及 underscore 1.8.3 _.isNaN 的一个 BUG

    这篇文章并不在我的 underscore 源码解读计划中,直到 @pod4g 同学回复了我的 issue(详见 https://github.com/hanzichi/underscore-analy ...