面试题目——《CC150》链表



面试题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》链表的更多相关文章
- 面试大总结:Java搞定面试中的链表题目总结
package LinkedListSummary; import java.util.HashMap; import java.util.Stack; /** * http://blog.csdn. ...
- (转)面试大总结之一:Java搞定面试中的链表题目
面试大总结之一:Java搞定面试中的链表题目 分类: Algorithm Interview2013-11-16 05:53 11628人阅读 评论(40) 收藏 举报 链表是面试中常出现的一类题目, ...
- C/C++ 笔试、面试题目大汇总 转
C/C++ 笔试.面试题目大汇总 这些东西有点烦,有点无聊.如果要去C++面试就看看吧.几年前网上搜索的.刚才看到,就整理一下,里面有些被我改了,感觉之前说的不对或不完善. 1.求下面函数的返回值( ...
- 前端面试题目汇总摘录(JS 基础篇)
JS 基础 JavaScript 的 typeof 返回那些数据类型 object number function boolean undefined string typeof null; // o ...
- (转)喜马拉雅2018 Java面试题目
背景:将网上的题目整理下. java基础 1:hashTable hashMap ConcurrentHashMap 的区别.数据结构.线程安全 2:equals和==区别, 重写equals一定要重 ...
- C++程序员面试题目总结(涉及C++基础、多线程多进程、网络编程、数据结构与算法)
说明:C++程序员面试题目总结(涉及C++基础知识.多线程多进程.TCP/IP网络编程.Linux操作.数据结构与算法) 内容来自作者看过的帖子或者看过的文章,个人整理自互联网,如有侵权,请联系作者 ...
- C语言经典面试题目(转的,不过写的的确好!)
第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量在这一 ...
- linux面试题目—2
linux面试题目—2 二 选择题 1.关闭linux系统(不重新启动)可使用命令 B . A Ctrl+Alt+Del B halt C shutdown -r now D reboot 2.实现从 ...
- linux面试题目--1
Linux面试题目 填空题1. 在Linux系统中,以 (文件)方式访问设备 .2. Linux内核引导时,从文件/etc/fstab 中读取要加载的文件系统.3. Linux文件系统中每个文件用i节 ...
- vc面试题目
class B { public: B() { cout << "default constructor" << endl; } ~B() { cout & ...
随机推荐
- IntelliJ IDEA 配置运行程序
IntelliJ IDEA 对于Javaer开发来说还是很nice的,就是第一次用可能配置项有点生疏,这里就记录一下IntelliJ IDEA 配置运行程序. 1. 点击Edit Config... ...
- MongoDB学习笔记~大叔分享批量添加—批量更新—批量删除
回到目录 说它是批量操作,就是说将集合对象一次提交到服务器,并对数据进行持久化,如果您的代码是一次一次的提交,那不算是批量操作!在之前的mongodb仓储中并没有对批量更新和批量删除进行实现,而今天在 ...
- 问题解决——MFC Ribbon 添加图标
=================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载 请通过右 ...
- ubuntu下设置开机启动服务
原文:http://blog.csdn.net/dante_k7/article/details/7213151 在ubuntu10.04之前的版本都是使用chkconfig来进行管理,而在之后的版本 ...
- stm32 u8 u16 u32
u8 是 unsigned char u16 是 unsigned short u32 是 unsigned int
- linux安装VirualBox虚拟机
第一步:安装VNC 1. 安装vnc yum install -y tigervnc* 2.启动vncserver [root@xxx ~]# vncserver You will require a ...
- 《InsideUE4》-10-GamePlay架构(九)GameInstance
一人之下,万人之上 引言 上篇我们讲到了UE在World之上,继续抽象出了Player的概念,包含了本地的ULocalPlayer和网络的UNetConnection,并以此创建出了World中的Pl ...
- Machine Learning Algorithms Study Notes(5)—Reinforcement Learning
Reinforcement Learning 对于控制决策问题的解决思路:设计一个回报函数(reward function),如果learning agent(如上面的四足机器人.象棋AI程序)在决定 ...
- Mysql数据库主从心得整理
管理mysql主从有2年多了,管理过200多组mysql主从,几乎涉及到各个版本的主从,本博文属于总结性的,有一部分是摘自网络,大部分是根据自己管理的心得和经验所写,整理了一下,分享给各位同行,希望对 ...
- java并发编程学习:用 Semaphore (信号量)控制并发资源
并发编程这方面以前关注得比较少,恶补一下,推荐一个好的网站:并发编程网 - ifeve.com,上面全是各种大牛原创或编译的并发编程文章. 今天先来学习Semaphore(信号量),字面上看,根本不知 ...