JAVA实现链表面试题.doc

上传人:scccc 文档编号:11943576 上传时间:2021-11-10 格式:DOC 页数:38 大小:442KB
返回 下载 相关 举报
JAVA实现链表面试题.doc_第1页
第1页 / 共38页
JAVA实现链表面试题.doc_第2页
第2页 / 共38页
JAVA实现链表面试题.doc_第3页
第3页 / 共38页
JAVA实现链表面试题.doc_第4页
第4页 / 共38页
JAVA实现链表面试题.doc_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《JAVA实现链表面试题.doc》由会员分享,可在线阅读,更多相关《JAVA实现链表面试题.doc(38页珍藏版)》请在三一文库上搜索。

1、JAVA实现链表面试题这篇文章主要介绍了 JAVA相关实现链表的面试题,代码实现非常详细,每一个方法讲解也 很到位,特别适合参加Java面试的朋友阅读。这份笔记整理了整整一个星期,每一行代码都是自己默写完成,并测试运行成功,同时也回顾了一下剑指 offer这本书中和链表有关的讲解,希望对笔试和面试有所帮助。本文包含链表的以下内容:1、单链表的创建和遍历2、求单链表中节点的个数3、 查找单链表中的倒数第k个结点(剑指offer,题15)4、查找单链表中的中间结点5、合并两个有序的单链表,合并之后的链表依然有序【出现频率高】(剑指offer,题17)6、 单链表的反转【出现频率最高】(剑指offe

2、r,题16)7、从尾到头打印单链表(剑指 offer,题5)8、判断单链表是否有环9、取出有环链表中,环的长度10、 单链表中,取出环的起始点(剑指 offer,题56)。本题需利用上面的第 8题和第9 题。11、判断两个单链表相交的第一个交点(剑指 offer,题37)1、单链表的创建和遍历:1public class Lin kList 2public Node head;3public Node curre nt;4/方法:向链表中添加数据5public void add(i nt data) 6/判断链表为空的时候7if (head = null) /如果头结点为空,说明这个链表还没有

3、创建,那就把新的结点赋给头8结点9head = new Node(data);10curre nt = head;11 else 12/创建新的结点,放在当前节点的后面(把新的结点合链表进仃关联)13curre nt.n ext = new Node(data);14/把链表的当前索引向后移动一位15current = current.next; /此步操作完成之后,current结点指向新添加的那个结点161718/方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进仃遍历19public void prin t(Node no de) 20if (node = n ull) 2

4、1return;2223curre nt = no de;24while (curre nt != nu II) 25System.out.pri ntln( curre nt.data);26curre nt = curre nt. next;272829class Node 30/注:此处的两个成员变量权限不能为private,因为private的权限是仅对本类访冋。31int data; / 数据域32Node next;/ 指针域33public Node( int data) 34this.data = data;353637public static void main( Stri

5、 ng args) 38Lin kList list = new Lin kList();39/向LinkList中添加数据40for (int i = 0; i 10; i+) 41list.add(i);4243list.print(list.head);/从 head 节点开始遍历输出44454647484950515253上方代码中,这里面的Node节点采用的是内部类来表示(33行)。使用内部类的最大好处是可以和外部类进行私有操作的互相访问。注:内部类访问的特点是:内部类可以直接访问外部类的成员,包括私有;外部类要访问内部类的成员,必须先创建对象。为了方便添加和遍历的操作,在LinkL

6、ist类中添加一个成员变量 current,用来表示当前节点的索引(03 行)。这里面的遍历链表的方法(20行)中,参数node表示从node节点开始遍历,不一定要从 head节点遍历。2、求单链表中节点的个数:注意检查链表是否为空。时间复杂度为0 (n)。这个比较简单。核心代码:123456789101112131415/方法:获取单链表的长度public int getLe ngth(Node head) if (head = n ull) return 0;int len gth = 0;Node curre nt = head;while (curre nt != nu II) len

7、 gth+;curre nt = curre nt. next;return len gth;3、查找单链表中的倒数第k个结点:3.1普通思路:先将整个链表从头到尾遍历一次, 计算出链表的长度 size,得到链表的长度之后, 就好办了, 直接输出第(size-k)个节点就可以了(注意链表为空,k为0, k为1, k大于链表中节点个数时的情况)。时间复杂度为 0 (n),大概思路如下:1920123456789101112131415161718public int findLastNode(int index) /index 代表的是倒数第index 的那个结点/第一次遍历,得到链表的长度si

8、zeif (head = n ull) return -1;curre nt = head;while (curre nt != nu II) size+;curre nt = curre nt. next;/第二次遍历,输出倒数第index个结点的数据curre nt = head;for (int i = 0; i size - in dex; i+) curre nt = curre nt. next;return curre nt.data;211234567891011121314151617181920212223如果面试官不允许你遍历链表的长度,该怎么做呢?接下来就是。3.2改进

9、思路:(这种思路在其他题目中也有应用)这里需要声明两个指针:即两个结点型的变量 first和seco nd,首先让first和seco nd都指向 第一个结点,然后让 seco nd结点往后挪k-1个位置,此时first和seco nd就间隔了 k-1个位 置,然后整体向后移动这两个节点,直到seco nd节点走到最后一个结点的时候,此时first节点所指向的位置就是倒数第 k个节点的位置。时间复杂度为 0 (n)代码实现:(初版)public Node fin dLastNode(Node head, int in dex) if (node = n ull) return nu II;No

10、de first = head;Node sec ond = head;/让seco nd结点往后挪in dex个位置for (int i = 0; i in dex; i+) sec ond = sec ond.n ext;/让first和seco nd结点整体向后移动,直到seco nd结点为Nullwhile (sec ond != nu II) first = first. next;sec ond = sec ond.n ext;/当seco nd结点为空的时候,此时first指向的结点就是我们要找的结点return first;代码实现:(最终版)(考虑k大于链表中结点个数时的情况

11、时,抛出异常) 上面的代码中,看似已经实现了功能,其实还不够健壮:要注意k等于0的情况;如果k大于链表中节点个数时,就会报空指针异常,所以这里需要做一下判断。 核心代码如下:1 public Node fin dLastNode(Node head, i nt k) 2 if (k = 0 |head= null) 3 return nu II;4 5 Node first = head;6Node sec ond = head;7/让seco nd结点往后挪k-1个位置8for (int i = 0; i 2-3-4链表2:2- 3-4-5合并后:1-2-2-3-3-4-4-5解题思路:挨着

12、比较链表1和链表2。这个类似于归并排序。尤其要注意两个链表都为空、和其中一个为空的情况。只需要0(1)的空间。时间复杂度为O (max(len1,len2)代码实现:1:/两个参数代表的是两个链表的头结点2public Node mergeLi nkList(Node head1, Node head2) 3if (head1 - null & head2 - null) / 如果两个链表都为空4return nu II;56if (head1 = null) 7retur n head2;89if (head2 = null) 10retur n head1;1112Node head; /

13、新链表的头结点13Node curre nt; /curre nt 结点指向新链表14/ 一开始,我们让 current结点指向head1和head2中较小的数据,得到 head结点15if (head1.data head2.data) 16head = head1;17curre nt = head1;18head1 = head1. next;19 else 20head = head2;21curre nt = head2;22232425262728293031323334353637383940414243head2 = head2. next;while (head1 != nu

14、ll & head2 != n ull) if (head1.data head2.data) current.next = head1; /新链表中,current指针的下一个结点对应较小的那个数据 curre nt = curre nt.n ext; /curre nt 指针下移head1 = head1. next; else curre nt. next = head2;curre nt = curre nt. next;head2 = head2. next;/合并剩余的元素if (head1 != null) /说明链表2遍历完了,是空的curre nt.n ext = head1

15、;if (head2 != null) /说明链表1遍历完了,是空的curre nt. next = head2;retur n head;444546474849代码测试:public static void main( Stri ng 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思路:从头到尾遍历原链表,每遍历一个结点,将

16、其摘下放在新链表的最前端。注意链表为空和只有一个结点的情况。时间复杂度为0 (n)方法1:(遍历)14current = next; / 操作结束后,current节点后移1516return reverseHead;17181920212223上方代码中,核心代码是第 16、17行。方法2:(递归)这个方法有点难,先不讲了。7、从尾到头打印单链表:对于这种颠倒顺序的问题,我们应该就会想到栈,后进先出。所以,这一题要么自己使用栈,要么让系统使用栈,也就是递归。注意链表为空的情况。时间复杂度为0 ( n)注:不要想着先将单链表反转,然后遍历输出,这样会破坏链表的结构,不建议。方法1:(自己新建一

17、个栈)123/方法:从尾到头打印单链表4public void reversePri nt(Node head) 5if (head = n ull) 6return;78Stack stack = new Stack(); / 新建一个栈9Node curre nt = head;10/将链表的所有结点压栈11while (curre nt != nu II) -12stack.push(current); / 将当前结点压栈13curre nt = curre nt. next;1415/将栈中的结点打印输出即可16while (stack.size() 0) 17System.out.p

18、rintln(stack.pop().data); / 出栈操作18192021方法2:(使用系统的栈:递归,代码优雅简洁)1 public void reversePri nt(Node head) 12345678910111213141516171819202if (head = nu II) 3return;45reversePri nt(head. next);6System.out.pri ntln( head.data);789总结:方法2是基于递归实现的,戴安看起来简洁优雅,但有个问题:当链表很长的时候,就会导致方法调用的层级很深,有可能造成栈溢出。 而方法1的显式用栈,是基于

19、循环实现的,代码的鲁棒性要更好一些。&判断单链表是否有环:这里也是用到两个指针,如果一个链表有环, 那么用一个指针去遍历,是永远走不到头的。因此,我们用两个指针去遍历:first指针每次走一步,seco nd指针每次走两步,如果first 指针和seco nd指针相遇,说明有环。时间复杂度为0( n)。方法:/方法:判断单链表是否有环public boolea n hasCycle(Node head) if (head = n ull) return false;Node first = head;Node sec ond = head;while (sec ond != nu II) fi

20、rst = first .n ext; /first 指针走一步second = second.next.next; second 指针走两步if (first = seco nd) / 一旦两个指针相遇,说明链表是有环的 return true;return false;完整版代码:(包含测试部分)这里,我们还需要加一个重载的add(Node node)方法,在创建单向循环链表时要用到。1Lin kList.java:213456789101112131415161718192021222324252627282930313233343536373839404142434445public

21、class Lin kList public Node head;public Node curre nt;/方法:向链表中添加数据public void add(i nt data) /判断链表为空的时候if (head = null) /如果头结点为空,说明这个链表还没有创建,那就把新的结点赋给头结点head = new Node(data);curre nt = head; else /创建新的结点,放在当前节点的后面(把新的结点合链表进行关联)curre nt.n ext = new Node(data);/把链表的当前索引向后移动一位curre nt = curre nt. next

22、;/方法重载:向链表中添加结点public void add(Node no de) if (node = n ull) return;if (head = n ull) head = node;curre nt = head; else curre nt. next = no de;curre nt = curre nt. next;/方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进行遍历public void prin t(Node no de) if (node = n ull) return;curre nt = no de;while (curre nt != nu

23、II) System.out.pri ntln( curre nt.data);curre nt = curre nt. next;/方法:检测单链表是否有环public boolea n hasCycle(Node head) if (head = n ull) 46return false;Node first = head;Node sec ond = head;while (sec ond != nu II) first = first .n ext; /first 指针走一步second = second.next.next; /second 指针走两步if (first = sec

24、o nd) / 一旦两个指针相遇,说明链表是有环的 return true;return false;class Node /注:此处的两个成员变量权限不能为private,因为private的权限是仅对本类访问。int data; / 数据域Node next;/ 指针域public Node(int data) this.data = data;public static void main( Stri ng args) Lin kList list = new Lin kList(www.h unanwang.n et);/向LinkList中添加数据for (int i = 0; i

25、4; i+) list.add(i);list.add(list.head); /将头结点添加到链表当中,于是,单链表就有环了。备注:此时得 到的这个环的结构,是下面的第8小节中图1的那种结构。System.out.pri ntl n(list.hasCycle(list.head);47484950515253545556575859606162636465666768697071727374757677787980818283848586878889检测单链表是否有环的代码是第50行。88行:我们将头结点继续往链表中添加,此时单链表就环了。最终运行效果为true。如果删掉了 88行代码,此

26、时单链表没有环,运行效果为false。9、取出有环链表中,环的长度:我们平时碰到的有环链表是下面的这种:(图1)上图中环的长度是 4。 但有可能也是下面的这种:(图2)此时,上图中环的长度就是3 了。那怎么求出环的长度呢?思路:这里面,我们需要先利用上面的第 7小节中的hasCycle方法(判断链表是否有环的那个方法), 这个方法的返回值是 boolean型,但是现在要把这个方法稍做修改,让其返回值为相遇的那个结点。然后,我们拿到这个相遇的结点就好办了,这个结点肯定是在环里嘛,我们可以让 这个结点对应的指针一直往下走,直到它回到原点,就可以算出环的长度了。方法:1/方法:判断单链表是否有环。返

27、回的结点是相遇的那个结点2public Node hasCycle(Node head) 3if (head = n ull) 4return nu II;56Node first = head;7Node sec ond = head;8while (sec ond != nu II) 9101112131415161718192021222324252627282930313233first = first. next;sec ond = sec ond.n ext. next;if (first = seco nd) / 一旦两个指针相遇,说明链表是有环的return first; /将

28、相遇的那个结点进行返回return nu II;/方法:有环链表中,获取环的长度。参数 node代表的是相遇的那个结点public int getCycleLe ngth(Node no de) if (head = n ull) return 0;Node curre nt = no de;int len gth = 0;while (curre nt != nu II) curre nt = curre nt. next;len gth+;if (curre nt = no de) /当curre nt结点走至U原点的时候return len gth;return len gth;3435

29、3637383940完整版代码:(包含测试部分)public class Lin kList public Node head;public Node curre nt;public int size;/方法:向链表中添加数据public void add(i nt data) /判断链表为空的时候if (head = null) /如果头结点为空,说明这个链表还没有创建,那就把新的结点赋给头结点4710head = new Node(data);11121314151617181920212223242526272829303132333435363738394041424344454647

30、484950515253curre nt = head; else /创建新的结点,放在当前节点的后面(把新的结点合链表进行关联)curre nt.n ext = new Node(data);/把链表的当前索引向后移动一位current = current.next; /此步操作完成之后,current结点指向新添加的那个结点/方法重载:向链表中添加结点public void add(Node no de) if (node = n ull) return;if (head = n ull) head = node;curre nt = head; else curre nt. next =

31、 no de;curre nt = curre nt. next;/方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进行遍历public void prin t(Node no de) if (node = n ull) return;curre nt = no de;while (curre nt != nu II) System.out.pri ntln( curre nt.data);curre nt = curre nt. next;/方法:判断单链表是否有环。返回的结点是相遇的那个结点public Node hasCycle(Node head) if (head =

32、n ull) return nu II;Node first = head;Node sec ond = head;while (sec ond != nu II) first = first. next;sec ond = sec ond.n ext. next;if (first = seco nd) / 一旦两个指针相遇,说明链表是有环的5556575859606162636465666768697071727374757677787980818283848586878889909192939495969754return first; /将相遇的那个结点进行返回return nu II

33、;/方法:有环链表中,获取环的长度。参数node代表的是相遇的那个结点public int getCycleLe ngth(Node no de) if (head = n ull) return 0;Node curre nt = no de;int len gth = 0;while (curre nt != nu II) curre nt = curre nt. next;len gth+;if (curre nt = no de) /当curre nt结点走至U原点的时候return len gth;return len gth;class Node /注:此处的两个成员变量权限不能为

34、private,因为private的权限是仅对本类访问。int data; / 数据域Node next;/ 指针域public Node(int data) this.data = data;public static void main(String args) LinkList list1 = new LinkList();Node seco nd = n ull; /把第二个结点记下来/向LinkList中添加数据for (int i = 0; i 4; i+) list1.add(i);if (i = 1) second = list1.current; /把第二个结点记下来list1.add(seco nd); /将尾结点指向链表的第二个结点,于是单链表就有环了,备注:此 时得到的环的结构,是本

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1