资讯专栏INFORMATION COLUMN

我的面试准备过程--链表(更新中)

30e8336b8229 / 1237人阅读

摘要:拿了小米和美团的,要被延期,失效,工作重新找。把准备过程纪录下来,共勉。注意,有环的链表,此种方法失效。已知两个单链表和各自有序,把它们合并成一个链表依然有序

写在最前面

导师贪腐出逃美国,两年未归,可怜了我。拿了小米和美团的offer,要被延期,offer失效,工作重新找。把准备过程纪录下来,共勉。

链表是最常考察的数据结构

</>复制代码

  1. // 链表定义
  2. public class Node{
  3. int val;//数据域
  4. Node next;// 指针域
  5. public Node(int val){
  6. this.val = val;
  7. }
  8. }

基础题

求单链表的长度
考察链表遍历的方法,时间复杂度为o(n)

</>复制代码

  1. //求单链表长度,起手判断链表是否为空,非常必要
  2. public static int getListLength(Node head){
  3. //如果链表为空,长度为0
  4. if(null == head){
  5. return 0;
  6. }
  7. // 开始着手办正事了
  8. int len = 0;
  9. Node cur = head;
  10. //如果循环判断条件不确定,可以先写逻辑,回头再定条件
  11. while(null != cur.next){
  12. len++;
  13. cur = cur.next;
  14. }
  15. return len;
  16. }

结点相对位置关系,结点与链表的相对位置关系

翻转链表
链表中经典解法1,引入两个指针,然后确定其相对位置关系

迭代法
解题思路:前后两个指针,遍历链表,每遍历一个结点,前面的指针将指向后面的指针,时间复杂度o(n)

</>复制代码

  1. public static Node getReverseList(Node head){
  2. if(null == head || null == head.next){
  3. return null;
  4. }
  5. Node cur = head;
  6. Node newHead = null;
  7. while(null != cur){
  8. Node preCur = cur;
  9. cur = cur.next;
  10. preCur.next = newHead;
  11. newHead = preCur;
  12. }
  13. return newHead;
  14. }

返回单链表倒数第k个结点
两个指针,前面的指针超前后面指针k个位置

</>复制代码

  1. public static Node findLastKthNode(Node head,int k){
  2. if(k == 0 || null == head){
  3. return null;
  4. }
  5. Node p = head;
  6. Node q = head;
  7. while(k > 0 || p != null){
  8. p = p.next;
  9. k--;
  10. }
  11. if(k > 0 || p == null){
  12. return null;
  13. }
  14. while(p != null){
  15. q = q.next;
  16. p = p.next;
  17. }
  18. return q;
  19. }

查找中间结点
分析:两个指针可以解决,位置相对关系是:p指针向前移动一步,q指针向前移动两步

</>复制代码

  1. public static Node getMiddleNode(Node head){
  2. if(null == head || head.next == null){
  3. return head;
  4. }
  5. Node p = head;
  6. Node q = head;
  7. while(p != null){
  8. p = p.next;
  9. q = q.next;
  10. if(p != null){
  11. p = p.next;
  12. }
  13. }
  14. return q;
  15. }

从尾到头打印单链表

</>复制代码

  1. public static void printList(Node head){
  2. Stack s = new Stack<>();
  3. Node cur = head;
  4. while(cur != null){
  5. s.push(cur);
  6. cur = cur.next;
  7. }
  8. while(!s.empty()){
  9. cur = s.pop();
  10. System.out.print(p.val + " ");
  11. }
  12. System.out.println();
  13. }

有环问题

判断一个单链表是否有环
思路:快慢两个指针,如果相遇,则有环

</>复制代码

  1. public static boolean hasCycle(Node head){
  2. Node fast = head;
  3. Node slow = head;
  4. while(fast != null && fast.next != null){
  5. fast = fast.next.next;
  6. slow = slow.next;
  7. if(fast == slow){
  8. return true;
  9. }
  10. }
  11. return false;
  12. }

</>复制代码

  1. - **取出有环链表中,环的长度**

思路:找到相遇的结点a后,让b结点从a位置向下遍历,回来后得到的结点数,即为环长度

</>复制代码

  1. //方法:有环链表中,获取环的长度。参数node代表的是相遇的那个结点
  2. public int getCycleLength(Node node) {
  3. if(node == null){
  4. return 0;
  5. }
  6. int length = 0;
  7. Node cur = node;
  8. while(cur != null){
  9. cur = cur.next;
  10. length++;
  11. if(cur != node){
  12. return length;
  13. }
  14. }
  15. return length;
  16. }

判断两个链表相交
思路:如果两链接的尾结点相同,则有环。注意,有环的链表,此种方法失效。

</>复制代码

  1. public static boolean isIntersect(Node head1, Node head2) {
  2. Node tail1 = head1;
  3. Node tail2 = head2;
  4. while(tail1 != null){
  5. tail1 = tail1.next;
  6. }
  7. while(tail2 != null){
  8. tail2 = tail2.next;
  9. }
  10. if(tail1 == tail2){
  11. return true;
  12. }
  13. return false;
  14. }

单链表中,取出环的起始点

</>复制代码

  1. public Node getCycleStart(Node head, int cycleLength) {
  2. Node first = head;
  3. Node second = head;
  4. for(int i = 0; i < cycleLength; i++){
  5. second = second.next;
  6. }
  7. while(first != null && second != null){
  8. if(first == second){
  9. return first;
  10. }
  11. first = first.next;
  12. second = second.next;
  13. }
  14. return null;
  15. }

求两个单链表相交的第一个节点
思路:分别求出a链表、b链表的长度len1、len2,如果a链表比b链表长,则a提前移动len1-len2距离,然后a、b一起向前移动,直到引用相同。

</>复制代码

  1. public static Node getFirstCommonNode(Node head1, Node head2) {
  2. Node tail1 = head1;
  3. Node tail2 = head2;
  4. int len1 = 1;
  5. while(tail1 != null){
  6. tail1 = tail1.next;
  7. len1++;
  8. }
  9. int len2 = 1;
  10. while(tail2 != null){
  11. tail2 = tail2.next;
  12. len2++;
  13. }
  14. if(tail1 != tail2){
  15. return null;
  16. }
  17. Node n1 = head1;
  18. Node n2 = head2;
  19. if(len1 > len2){
  20. int k = len1 - len2;
  21. while(k != 0){
  22. n1 = n1.next;
  23. k--;
  24. }
  25. }
  26. if(len1 < len2){
  27. int k = len1 - len2;
  28. while(k != 0){
  29. n2 = n2.next;
  30. k--;
  31. }
  32. }
  33. while(n1 != n2){
  34. n1 = n1.next;
  35. n2 = n2.next;
  36. }
  37. return n1;
  38. }

已知两个单链表head1和head2各自有序,把它们合并成一个链表依然有序

</>复制代码

  1. public static Node mergeSortedList(Node head1, Node head2){
  2. if(head1 == null){
  3. return head2;
  4. }
  5. if(head2 == null){
  6. return head1;
  7. }
  8. Node mergeHead = null;
  9. if(head1.val < head2.val){
  10. mergeHead = head1;
  11. head1 = head1.next;
  12. mergeHead.next = null;
  13. }else{
  14. mergeHead = head2;
  15. head2 = head2.next;
  16. mergeHead.next = null;
  17. }
  18. Node mergeCur = mergeHead;
  19. while(head1 != null && head2 != null){
  20. if(head1.val < head2.val){
  21. mergeCur.next = head1;
  22. head1 = head1.next;
  23. mergeCur = mergeCur.next;
  24. mergeCur.next = null;
  25. }else{
  26. mergeCur.next = head2;
  27. head2 = head2.next;
  28. mergeCur = mergeCur.next;
  29. mergeCur.next = null;
  30. }
  31. }
  32. if(head1 != null){
  33. mergeCur.next = head1;
  34. }
  35. if(head2 != null){
  36. mergeCur.next = head2;
  37. }
  38. return mergeHead;
  39. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70116.html

相关文章

  • 我的面试准备过程--容器(更新)

    摘要:底层实现是对象数组,优点是时间为,缺点是和时间为,需要留意的是扩容的过程以及的算法本节参考源码中放最新的源码为,组成链表或红黑树定义从整体上看,底层的存储结构是基于数组和链表实现的。实现了所谓的线程安全,在很多方法上都加上了。 ArrayList ArrayList底层实现是对象数组,优点是set、get时间为O(1),缺点是add和remove时间为O(n),需要留意的是扩容的过程以...

    zhisheng 评论0 收藏0
  • 春招:我居然三天就拿到了offer?

    摘要:算法名称描述优点缺点标记清除算法暂停除了线程以外的所有线程算法分为标记和清除两个阶段首1 回顾我的时间线 在本文的开头,先分享一下自己的春招经历吧: 各位掘友大家好,我是练习时长快一年的Android小蔡鸡,喜欢看源码,逛掘金,写技术文章...... 好了好,不开玩笑了OWO,今年春招投了许多简历的,但是被捞的只有阿里,头条和美团,一路下来挺不容易的. 个人认为在春招中运气>性格>三观>技术...

    stormjun 评论0 收藏0
  • 我的面试准备过程--队列与栈(更新)

    摘要:和三个方法的时间复杂度必须为两种解法,解法一,将最小值存入自有的数据结构中,如下所示原本的值最小值解法二,用两个栈 堆栈和队列统称线性表 简单的线性结构 数组和链表可以实现这两种数据结构 堆栈 基本理解 DFS 深度优先---按深度遍历 递归转非递归 队列 基本理解 BFS 广度优先---按层序遍历 出入栈的合法性模拟出入栈的过程,不是入栈,就是...

    EastWoodYang 评论0 收藏0
  • 我的面试准备过程--leetcode树

    摘要:由遍历结果反求树分析递归分治,第一层需要找到相应的遍历结果,对数组来说,问题转化为找下标问题对前序中序遍历结果来说前序左右中序左右因此,中序中的下标可求,为对每一层来说,左子树的长度为,右子树的长度为,左子树前序为至,中序为至,可以使  由遍历结果反求树 分析:递归分治,第一层需要找到相应的遍历结果,对数组来说,问题转化为找下标问题 对前序、中序遍历结果来说 前序:[root,[左...

    wenyiweb 评论0 收藏0

发表评论

0条评论

30e8336b8229

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<