JAVA实现链表面试题.doc_第1页
JAVA实现链表面试题.doc_第2页
JAVA实现链表面试题.doc_第3页
JAVA实现链表面试题.doc_第4页
JAVA实现链表面试题.doc_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JAVA实现链表面试题这篇文章主要介绍了JAVA相关实现链表的面试题,代码实现非常详细,每一个方法讲解也很到位,特别适合参加Java面试的朋友阅读。这份笔记整理了整整一个星期,每一行代码都是自己默写完成,并测试运行成功,同时也回顾了一下剑指offer这本书中和链表有关的讲解,希望对笔试和面试有所帮助。本文包含链表的以下内容:1、单链表的创建和遍历2、求单链表中节点的个数3、查找单链表中的倒数第k个结点(剑指offer,题15)4、查找单链表中的中间结点5、合并两个有序的单链表,合并之后的链表依然有序【出现频率高】(剑指offer,题17)6、单链表的反转【出现频率最高】(剑指offer,题16)7、从尾到头打印单链表(剑指offer,题5)8、判断单链表是否有环9、取出有环链表中,环的长度10、单链表中,取出环的起始点(剑指offer,题56)。本题需利用上面的第8题和第9题。11、判断两个单链表相交的第一个交点(剑指offer,题37)1、单链表的创建和遍历:?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253public class LinkList public Node head;public Node current;/方法:向链表中添加数据public void add(int data) /判断链表为空的时候if (head = null) /如果头结点为空,说明这个链表还没有创建,那就把新的结点赋给头结点head = new Node(data);current = head; else /创建新的结点,放在当前节点的后面(把新的结点合链表进行关联)current.next = new Node(data);/把链表的当前索引向后移动一位current = current.next; /此步操作完成之后,current结点指向新添加的那个结点/方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进行遍历public void print(Node node) if (node = null) return;current = node;while (current != null) System.out.println(current.data);current = current.next; class Node /注:此处的两个成员变量权限不能为private,因为private的权限是仅对本类访问。int data; /数据域Node next;/指针域public Node(int data) this.data = data;public static void main(String args) LinkList list = new LinkList();/向LinkList中添加数据for (int i = 0; i 10; i+) list.add(i);list.print(list.head);/ 从head节点开始遍历输出上方代码中,这里面的Node节点采用的是内部类来表示(33行)。使用内部类的最大好处是可以和外部类进行私有操作的互相访问。注:内部类访问的特点是:内部类可以直接访问外部类的成员,包括私有;外部类要访问内部类的成员,必须先创建对象。为了方便添加和遍历的操作,在LinkList类中添加一个成员变量current,用来表示当前节点的索引(03行)。这里面的遍历链表的方法(20行)中,参数node表示从node节点开始遍历,不一定要从head节点遍历。2、求单链表中节点的个数:注意检查链表是否为空。时间复杂度为O(n)。这个比较简单。核心代码:?123456789101112131415/方法:获取单链表的长度public int getLength(Node head) if (head = null) return 0;int length = 0;Node current = head;while (current != null) length+;current = current.next;return length;3、查找单链表中的倒数第k个结点:3.1 普通思路:先将整个链表从头到尾遍历一次,计算出链表的长度size,得到链表的长度之后,就好办了,直接输出第(size-k)个节点就可以了(注意链表为空,k为0,k为1,k大于链表中节点个数时的情况)。时间复杂度为O(n),大概思路如下:?123456789101112131415161718192021public int findLastNode(int index) /index代表的是倒数第index的那个结点/第一次遍历,得到链表的长度sizeif (head = null) return -1;current = head;while (current != null) size+;current = current.next;/第二次遍历,输出倒数第index个结点的数据current = head;for (int i = 0; i size - index; i+) current = current.next;return current.data;如果面试官不允许你遍历链表的长度,该怎么做呢?接下来就是。3.2 改进思路:(这种思路在其他题目中也有应用)这里需要声明两个指针:即两个结点型的变量first和second,首先让first和second都指向第一个结点,然后让second结点往后挪k-1个位置,此时first和second就间隔了k-1个位置,然后整体向后移动这两个节点,直到second节点走到最后一个结点的时候,此时first节点所指向的位置就是倒数第k个节点的位置。时间复杂度为O(n)代码实现:(初版)?1234567891011121314151617181920212223public Node findLastNode(Node head, int index) if (node = null) return null;Node first = head;Node second = head;/让second结点往后挪index个位置for (int i = 0; i index; i+) second = second.next;/让first和second结点整体向后移动,直到second结点为Nullwhile (second != null) first = first.next;second = second.next;/当second结点为空的时候,此时first指向的结点就是我们要找的结点return first;代码实现:(最终版)(考虑k大于链表中结点个数时的情况时,抛出异常)上面的代码中,看似已经实现了功能,其实还不够健壮:要注意k等于0的情况;如果k大于链表中节点个数时,就会报空指针异常,所以这里需要做一下判断。核心代码如下:?1234567891011121314151617181920212223242526public Node findLastNode(Node head, int k) if (k = 0 | head = null) return null;Node first = head;Node second = head;/让second结点往后挪k-1个位置for (int i = 0; i 2-3-4链表2:2-3-4-5合并后:1-2-2-3-3-4-4-5解题思路:挨着比较链表1和链表2。这个类似于归并排序。尤其要注意两个链表都为空、和其中一个为空的情况。只需要O (1) 的空间。时间复杂度为O (max(len1,len2)代码实现:?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849/两个参数代表的是两个链表的头结点public Node mergeLinkList(Node head1, Node head2) if (head1 = null & head2 = null) /如果两个链表都为空return null;if (head1 = null) return head2;if (head2 = null) return head1;Node head; /新链表的头结点Node current; /current结点指向新链表 / 一开始,我们让current结点指向head1和head2中较小的数据,得到head结点if (head1.data head2.data) head = head1;current = head1;head1 = head1.next; else head = head2;current = head2;head2 = head2.next;while (head1 != null & head2 != null) if (head1.data head2.data) current.next = head1; /新链表中,current指针的下一个结点对应较小的那个数据current = current.next; /current指针下移head1 = head1.next; else current.next = head2;current = current.next;head2 = head2.next;/合并剩余的元素if (head1 != null) /说明链表2遍历完了,是空的current.next = head1;if (head2 != null) /说明链表1遍历完了,是空的current.next = head2;return head;代码测试:?1234567891011121314151617public static void main(String args) LinkList list1 = new LinkList();LinkList list2 = new LinkList();/向LinkList中添加数据for (int i = 0; i 4; i+) list1.add(i);for (int i = 3; i 2-3-4反转之后:4-2-2-1思路:从头到尾遍历原链表,每遍历一个结点,将其摘下放在新链表的最前端。注意链表为空和只有一个结点的情况。时间复杂度为O(n)方法1:(遍历)?1234567891011121314151617181920212223/方法:链表的反转public Node reverseList(Node head) /如果链表为空或者只有一个节点,无需反转,直接返回原链表的头结点if (head = null | head.next = null) return head;Node current = head;Node next = null; /定义当前结点的下一个结点Node reverseHead = null; /反转后新链表的表头while (current != null) next = current.next; /暂时保存住当前结点的下一个结点,因为下一次要用current.next = reverseHead; /将current的下一个结点指向新链表的头结点reverseHead = current; current = next; / 操作结束后,current节点后移return reverseHead;上方代码中,核心代码是第16、17行。方法2:(递归)这个方法有点难,先不讲了。7、从尾到头打印单链表:对于这种颠倒顺序的问题,我们应该就会想到栈,后进先出。所以,这一题要么自己使用栈,要么让系统使用栈,也就是递归。注意链表为空的情况。时间复杂度为O(n)注:不要想着先将单链表反转,然后遍历输出,这样会破坏链表的结构,不建议。方法1:(自己新建一个栈)?123456789101112131415161718192021/方法:从尾到头打印单链表public void reversePrint(Node head) if (head = null) return;Stack stack = new Stack(); /新建一个栈Node current = head;/将链表的所有结点压栈while (current != null) -stack.push(current); /将当前结点压栈current = current.next;/将栈中的结点打印输出即可while (stack.size() 0) System.out.println(stack.pop().data); /出栈操作方法2:(使用系统的栈:递归,代码优雅简洁)?123456789public void reversePrint(Node head) if (head = null) return;reversePrint(head.next);System.out.println(head.data);总结:方法2是基于递归实现的,戴安看起来简洁优雅,但有个问题:当链表很长的时候,就会导致方法调用的层级很深,有可能造成栈溢出。而方法1的显式用栈,是基于循环实现的,代码的鲁棒性要更好一些。8、判断单链表是否有环:这里也是用到两个指针,如果一个链表有环,那么用一个指针去遍历,是永远走不到头的。因此,我们用两个指针去遍历:first指针每次走一步,second指针每次走两步,如果first指针和second指针相遇,说明有环。时间复杂度为O (n)。方法:?123456789101112131415161718192021/方法:判断单链表是否有环public boolean hasCycle(Node head) if (head = null) return false;Node first = head;Node second = head;while (second != null) first = first.next; /first指针走一步second = second.next.next; second指针走两步if (first = second) /一旦两个指针相遇,说明链表是有环的return true;return false;完整版代码:(包含测试部分)这里,我们还需要加一个重载的add(Node node)方法,在创建单向循环链表时要用到。?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889LinkList.java:public class LinkList public Node head;public Node current;/方法:向链表中添加数据public void add(int data) /判断链表为空的时候if (head = null) /如果头结点为空,说明这个链表还没有创建,那就把新的结点赋给头结点head = new Node(data);current = head; else /创建新的结点,放在当前节点的后面(把新的结点合链表进行关联)current.next = new Node(data);/把链表的当前索引向后移动一位current = current.next;/方法重载:向链表中添加结点public void add(Node node) if (node = null) return;if (head = null) head = node;current = head; else current.next = node;current = current.next;/方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进行遍历public void print(Node node) if (node = null) return;current = node;while (current != null) System.out.println(current.data);current = current.next;/方法:检测单链表是否有环public boolean hasCycle(Node head) if (head = null) return false;Node first = head;Node second = head;while (second != null) first = first.next; /first指针走一步second = second.next.next; /second指针走两步if (first = second) /一旦两个指针相遇,说明链表是有环的return true;return false;class Node /注:此处的两个成员变量权限不能为private,因为private的权限是仅对本类访问。int data; /数据域Node next;/指针域public Node(int data) this.data = data;public static void main(String args) LinkList list = new LinkList();/向LinkList中添加数据for (int i = 0; i 4; i+) list.add(i);list.add(list.head); /将头结点添加到链表当中,于是,单链表就有环了。备注:此时得到的这个环的结构,是下面的第8小节中图1的那种结构。System.out.println(list.hasCycle(list.head);检测单链表是否有环的代码是第50行。88行:我们将头结点继续往链表中添加,此时单链表就环了。最终运行效果为true。如果删掉了88行代码,此时单链表没有环,运行效果为false。9、取出有环链表中,环的长度:我们平时碰到的有环链表是下面的这种:(图1)上图中环的长度是4。但有可能也是下面的这种:(图2)此时,上图中环的长度就是3了。那怎么求出环的长度呢?思路:这里面,我们需要先利用上面的第7小节中的hasCycle方法(判断链表是否有环的那个方法),这个方法的返回值是boolean型,但是现在要把这个方法稍做修改,让其返回值为相遇的那个结点。然后,我们拿到这个相遇的结点就好办了,这个结点肯定是在环里嘛,我们可以让这个结点对应的指针一直往下走,直到它回到原点,就可以算出环的长度了。方法:?12345678910111213141516171819202122232425262728293031323334353637383940/方法:判断单链表是否有环。返回的结点是相遇的那个结点public Node hasCycle(Node head) if (head = null) return null;Node first = head;Node second = head;while (second != null) first = first.next;second = second.next.next;if (first = second) /一旦两个指针相遇,说明链表是有环的return first; /将相遇的那个结点进行返回return null;/方法:有环链表中,获取环的长度。参数node代表的是相遇的那个结点public int getCycleLength(Node node) if (head = null) return 0;Node current = node;int length = 0;while (current != null) current = current.next;length+;if (current = node) /当current结点走到原点的时候return length;return length;完整版代码:(包含测试部分)?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899public class LinkList public Node head;public Node current;public int size;/方法:向链表中添加数据public void add(int data) /判断链表为空的时候if (head = null) /如果头结点为空,说明这个链表还没有创建,那就把新的结点赋给头结点head = new Node(data);current = head; else /创建新的结点,放在当前节点的后面(把新的结点合链表进行关联)current.next = new Node(data);/把链表的当前索引向后移动一位current = current.next; /此步操作完成之后,current结点指向新添加的那个结点/方法重载:向链表中添加结点public void add(Node node) if (node = null) return;if (head = null) head = node;current = head; else current.next = node;current = current.next;/方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进行遍历public void print(Node node) if (node = null) return;current = node;while (current != null) System.out.println(current.data);current = current.next;/方法:判断单链表是否有环。返回的结点是相遇的那个结点public Node hasCycle(Node head) if (head = null) return null;Node first = head;Node second = head;while (second != null) first = first.next;second = second.next.next;if (first = second) /一旦两个指针相遇,说明链表是有环的return first; /将相遇的那个结点进行返回return null;/方法:有环链表中,获取环的长度。参数node代表的是相遇的那个结点public int getCycleLength(Node node) if (head = null) return 0;Node current = node;int length = 0;while (current != null) current = current.next;length+;if (current = node) /当current结点走到原点的时候return length;return length;class Node /注:此处的两个成员变量权限不能为private,因为private的权限是仅对本类访问。int data; /数据域Node next;/指针域public Node(int data) this.data = data;public static void main(String args) LinkList list1 = new LinkList();Node second = null; /把第二个结点记下来/向LinkList中添加数据for (int i = 0; i 4; i+) list1.add(i);if (i = 1) second = list1.current; /把第二个结点记下来list1.add(second); /将尾结点指向链表的第二个结点,于是单链表就有环了,备注:此时得到的环的结构,是本节中图2的那种结构Node current = list1.hasCycle(list1.head); /获取相遇的那个结点System.out.println(环的长度为 + list1.getCycleLength(current);运行效果:如果将上面的104至122行的测试代码改成下面这样的:(即:将图2中的结构改成图1中的结构)?12345678910111213public static void main(String args) LinkList list1 = new LinkList();/向LinkList中添加数据for (int i = 0; i 4; i+) list1.add(i);list1.add(list1.head); /将头结点添加到链表当中(将尾结点指向头结点),于是,单链表就有环了。备注:此时得到的这个环的结构,是本节中图1的那种结构。Node current = list1.hasCycle(list1.head);System.out.println(环的长度为 + list1.getCycleLength(current); 运行结果:如果把上面的代码中的第8行删掉,那么这个链表就没有环了,于是运行的结果为0。10、单链表中,取出环的起始点:我们平时碰到的有环链表是下面的这种:(图1)上图中环的起始点1。但有可能也是下面的这种:(图2)此时,上图中环的起始点是2。方法1:这里我们需要利用到上面第8小节的取出环的长度的方法getCycleLength,用这个方法来获取环的长度length。拿到环的长度length之后,需要用到两个指针变量first和second,先让second指针走length步;然后让first指针和second指针同时各走一步,当两个指针相遇时,相遇时的结点就是环的起始点。注:为了找到环的起始点,我们需要先获取环的长度,而为了获取环的长度,我们需要先判断是否有环。所以这里面其实是用到了三个方法。代码实现:方法1的核心代码:?1234567891011121314151617181920212223242526/方法:获取环的起始点。参数length表示环的长度public Node getCycleStart(Node head, int cycleLength) if (head = null) return null;Node first = head;Node second = head;/先让second指针走length步for (int i = 0; i cycleLength; i+) second = second.next;/然后让first指针和second指针同时各走一步while (first != null & second != null) first = first.next;second = second.next;if (first = second) /如果两个指针相遇了,说明这个结点就是环的起始点return first;return null;完整版代码:(含测试部分)?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120public class LinkList public Node head;public Node current;public int size;/方法:向链表中添加数据public void add(int data) /判断链表为空的时候if (head = null) /如果头结点为空,说明这个链表还没有创建,那就把新的结点赋给头结点head = new Node(data);current = head; else /创建新的结点,放在当前节点的后面(把新的结点合链表进行关联)current.next = new Node(data);/把链表的当前索引向后移动一位current = current.next; /此步操作完成之后,current结点指向新添加的那个结点/方法重载:向链表中添加结点public void add(Node node) if (node = null) return;if (head = null) head = node;current = head; else current.next = node;current = current.next;/方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进行遍历public void print(Node node) if (node = null) return;current = node;while (current != null) System.out.println(current.data);current = current.next;/方法:判断单链表是否有环。返回的结点是相遇的那个结点public Node hasCycle(Node head) if (head = null) return null;Node first = head;Node second = head;while (second != null) first = first.next;second = second.next.next;if (first = second) /一旦两个指针相遇,说明链表是有环的return first; /将相遇的那个结点进行返回return null;/方法:有环链表中,获取环的长度。参数node代表的是相遇的那个结点public int getCycleLength(Node node) if (head = null) return 0;Node current = node;int length = 0;while (current != null) current = current.next;length+;if (current = node) /当current结点走到原点的时候return length;return length;/方法:获取环的起始点。参数length表示环的长度public Node getCycleStart(Node head, int cycleLength) if (head = null) return null;Node first = head;Node second = head;/先让second指针走length步for (int i = 0; i cycleLength; i+) second = second.next;/然后让first指针和second指针同时各走一步while (first != null & second != null) first = first.next;second = second.next;if (first = second) /如果两个指针相遇了,说明这个结点就是环的起始点return first;return null; class Node /注:此处的两个成员变量权限不能为private,因为private的权限是仅对本类访问。int data; /数据域Node next;/指针域publ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论