面试题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. 不好的MySQL过程编写习惯

    刚才为了测试一个东西,写了个存储过程: delimiter $$ drop procedure if exists sp_test$$ create procedure sp_test() begin ...

  2. 交叉编译总结 libosscore.a libcurl.a libmysqlclient.a

    把工程文件交叉编译到arm上运行,着实花费了一番功夫. 首先遇到的错误是 X uses VFP register arguments, B does not 百度了一下,发现是硬浮点和软浮点的问题,原 ...

  3. 打开Application Data

    1.建后缀名为reg的新文件,复制以下代码后点击运行. Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\*\shell\runas]@=& ...

  4. 使用 python 获取 Linux 系统信息(通过dmidecode命令)

    通过 dmidecode 命令可以获取到 Linux 系统的包括 BIOS. CPU.内存等系统的硬件信息,这里使用 python 代码来通过调用 dmidecode 命令来获取 Linux 必要的系 ...

  5. Neutron 理解 (7): Neutron 是如何实现负载均衡器虚拟化的 [LBaaS V1 in Juno]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  6. TJ2016 CTF Write up

    No zuo no die. Use markdown to write writeup in the future......

  7. 重构与模式(Refactoring to Patterns)-读书笔记

    第一章 ☛过度设计:是指代码的灵活性和复杂性超出所需. 第二章 ☛重构是一种保持行为的转换. 第三章 ☛每一个模式都是由三部分组成的规则,他表达的是某一环境,一个问题以及解决问题的方案之间的关系. ☛ ...

  8. BZOJ1500: [NOI2005]维修数列[splay ***]

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 12278  Solved: 3880[Submit][Statu ...

  9. 细说 C# 中的 IEnumerable和IEnumerator接口

    我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...

  10. angular的跨域(angular百度下拉提示模拟)和angular选项卡

    1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...