链表问题专项

1.快慢指针

package class03;public class LinkedListMid {public static class Node{public int value;public Node next;public Node(int v){value = v;}}public static Node midOrUpMidNode(Node head){if(head==null||head.next==null||head.next.next==null){return head;}Node slow = head.next;Node fast = head.next.next;while (fast.next!=null&&fast.next.next!=null){slow = slow.next;fast = fast.next.next;}return slow;}public static Node midOrDownMidNode(Node head){if(head==null||head.next==null){return head;}Node slow = head.next;Node fast = head.next;while (fast.next!=null&&fast.next.next!=null){slow = slow.next;fast = fast.next.next;}return slow;}public static Node minOrUpMidPreNode(Node head){if(head==null||head.next==null||head.next.next==null){return null;}Node slow = head;Node fast = head.next.next;while (fast.next!=null&&fast.next.next!=null){slow=slow.next;fast = fast.next.next;}return slow;}public static Node midOrDownMidPreNode(Node head){if(head==null||head.next==null){return null;}if(head.next.next==null){return head;}Node slow = head;Node fast = head.next;while (fast.next!=null&&fast.next.next!=null){slow = slow.next;fast = fast.next.next;}return slow;}
}

2.回文结构

1)容器实现

实现思路

  1. 将链表中的元素全部入栈
  2. 再将每一个元素依次出栈与链表每一节点的元素依次进行比较
  3. 如果有一个不一样就直接返回false,全部一样则返回true

代码实现

//回文结构判断public static boolean idPalindrome1(Node head) {Stack<Node> stack = new Stack<>();Node cur = head;while (cur != null) {stack.push(cur);cur = cur.next;}while (head != null) {if (head.value != stack.pop().value) {return false;}head = head.next;}return true;}

2)仅使用一半的容器

实现思路

  1. 使用快慢指针,当快指针指向链表结尾最后一个元素的时候,慢指针指向链表中间
  2. 将链表中间节点的下一个元素直到结尾都存入栈中
  3. 依次出栈与链表节点一一比对
  4. 如果有一个不一样就直接返回false ; 如果全部一样直到慢指针指向的节点,则返回true

3)完全不使用容器

实现思路

  1. 使用快慢指针,当快指针指向链表结尾最后一个元素的时候,慢指针指向链表中间
  2. 将中间节点之后的元素逆序,如下:
  3. 从链表头部和尾部开始同时比对
  4. 如果有一个不一样就直接返回false ; 如果全部一样直到某个指针走到null,则返回true

代码实现

//不使用容器public static boolean idPalindrome2(Node head) {if(head==null||head.next==null){return true;}Node n1 = head;Node n2 = head;while (n2.next!=null&&n2.next.next!=null){n1 = n1.next;n2 = n2.next.next;}n2 = n1.next;n1.next = null;Node n3 = null;while(n2!=null){n3 = n2.next;n2.next = n1;n1 = n2;n2 = n3;}n3 = n1;n2=head;boolean res = true;while (n1!=null&&n2!=null){if(n1.value!=n2.value){res = false;break;}n1 = n1.next;n2 = n2.next;}n1 = n3.next;n3.next=null;while (n1!=null){n2 = n1.next;n1.next = n3;n3=n1;n1=n2;}return res;}

3.链表划分

1)做partition

实现思路

  • 荷兰国旗问题

代码实现

2)分区

实现思路

  1. 根据输入的pivot值划分区域,每个区域分别都有头结点和尾节点
  2. 小于区域的尾巴连等于区域的头,等于区域的尾巴连大于区域的头(注意判断小于区域是否存在 , 等于区域是否存在)

代码实现

public static Node listPartition(Node head, int pivot) {Node sH = null; //small headNode sT = null; //small tailNode eH = null; //equal headNode eT = null; //equal tailNode mH = null; //big headNode mT = null; //big tailNode next = null;while (head != null) {next = head.next;head.next = null;if (head.value < pivot) {if (sH == null) {sH = head;sT = head;} else {sT.next = head;sT = head;}} else if (head.value == pivot) {if (eH == null) {eH = head;eT = head;} else {eT.next = head;eT = head;}} else {if (mH == null) {mH = head;mT = head;} else {mT.next = head;mT = head;}}head = next;}//小于区域的尾巴连等于区域的头,等于区域的尾巴连大于区域的头//另外考虑是否有小于区域的问题if (sT != null) {sT.next = eH;eT = eT == null ? sT : eT;}if (eT != null) {eT.next = mH;}return sH != null ? sH : (eH != null ? eH : mH);}

4.特殊链表

public static class Node {public int value;public Node next;public Node rand;public Node(int data) {this.value = data;}}public static Node copyListWithRand(Node head) {HashMap<Node, Node> map = new HashMap<>();Node cur = head;while (cur != null) {map.put(cur, new Node(cur.value));cur = cur.next;}cur = head;while (cur != null) {//cur:老//map.get(cur):新map.get(cur).next = map.get(cur.next);map.get(cur).rand = map.get(cur.rand);cur = cur.next;}return map.get(head);}

5.可能有环的链表相交问题

注意 : 单链表只有一个next指针

实现思路

获取入环节点

  1. 使用快慢指针,fast一次走2步,slow一次走一步,如果连表有环,那么fast和slow一定会在链表某一处相遇
  2. 若有环,此时将fast指针重置到链表开头,变为一次走一步,同时slow也一次走一步,当fast与slow相遇的是时候,即为入环节点
public static Node getLoopNode(Node head) {if (head == null || head.next == null || head.next.next == null) {return null;}Node n1 = head.next;    //n1 -> slowNode n2 = head.next.next;   //n2 -> fastwhile (n1 != n2) {if (n2.next == null || n2.next.next == null) {return null;}n2 = n2.next.next;n1 = n1.next;}n2 = head;  //n2重新回到头结点while (n1 != n2) {n1 = n1.next;n1 = n2.next;}return n1;}

如果两个连表都无环,返回第一个相交节点,如果不想交则返回null

//如果两个连表都无环,返回第一个相交节点,如果不想交则返回nullpublic static Node noLoop(Node head1, Node head2) {if (head1 == null || head2 == null) {return null;}Node cur1 = head1;Node cur2 = head2;int n = 0;while (cur1.next != null) {n++;cur1 = cur1.next;}while (cur2.next != null) {n--;cur2 = cur2.next;}if (cur1 != cur2) {return null;}//重新定向//谁长,谁把头给cur1,谁短,谁把头给cue2cur1 = n > 0 ? head1 : head2;cur2 = cur1 == head1 ? head2 : head1;//n取绝对值,长链表先走n个节点,然后两个链表同时走n = Math.abs(n);while (n != 0) {n--;cur1 = cur1.next;}while (cur1 != cur2) {cur1 = cur1.next;cur2 = cur2.next;}return cur1;}

如果两个链表都有环,寻找相交节点

public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {Node cur1 = null;Node cur2 = null;if (loop1 == loop2) {cur1 = head1;cur2 = head2;int n = 0;while (cur1 != loop1) {n++;cur2 = cur2.next;}cur1 = n > 0 ? head1 : head2;cur2 = cur1 == head1 ? head2 : head1;n = Math.abs(n);while (n != 0) {n--;cur1 = cur1.next;}while (cur1 != cur2) {cur1 = cur1.next;cur2 = cur2.next;}return cur1;} else {cur1 = loop1.next;}while (cur1 != loop1) {if (cur1 == loop2) {return loop1;}cur1 = cur1.next;}return null;}

主方法

    //mainpublic static Node getIntersectNode(Node head1, Node head2) {if (head1 == null || head2 == null) {return null;}Node loop1 = getLoopNode(head1);Node loop2 = getLoopNode(head2);if (loop1 == null || loop2 == null) {return noLoop(head1, head2);}if (loop1 != null && loop2 != null) {return bothLoop(head1, loop1, head2, loop2);}return null;}

6.无头结点删除

表面删除

思路

  • 将要删除节点的下一个节点的数据覆盖到要删除的结点上,然后删除那个节点

带来的问题:无法删除最后一个节点

其实是无法真正的删掉的,无论什么方法,一定会存在很多问题.因此在面试的过程中,若果问到了这个问题,告诉面试官一定要给出头结点,并给出及格看似可能实则有问题的做法并作出解释.

【数据结构的魅力】005.链表问题专项相关推荐

  1. python定义链表节点_Python数据结构与算法之链表定义与用法实例详解【单链表、循环链表】...

    本文实例讲述了Python数据结构与算法之链表定义与用法.分享给大家供大家参考,具体如下: 本文将为大家讲解: (1)从链表节点的定义开始,以类的方式,面向对象的思想进行链表的设计 (2)链表类插入和 ...

  2. php数据结构课程---2、链表(php中 是如何实现单链表的(也就是php中如何实现对象引用的))...

    php数据结构课程---2.链表(php中 是如何实现单链表的(也就是php中如何实现对象引用的)) 一.总结 一句话总结: php是弱类型语言,变量即可表示数值,也可表示对象:链表节点的数据域的值就 ...

  3. java双链表基本方法_Java数据结构之双端链表原理与实现方法

    本文实例讲述了Java数据结构之双端链表原理与实现方法.分享给大家供大家参考,具体如下: 一.概述: 1.什么时双端链表: 链表中保持这对最后一个连点引用的链表 2.从头部插入 要对链表进行判断,如果 ...

  4. 数据结构(三)--链表

    数据结构(三)–链表 文章目录 数据结构(三)--链表 介绍 单链表 代码实现 翻转链表 取出倒数第n个有效节点 介绍 链表又分: 单链表 双链表 单链表 头节点不存储数据,所有操作临时引用指向hea ...

  5. java数据接口之链表_Java数据结构和算法之链表

    三.链表 链结点 在链表中,每个数据项都被包含在'点"中,一个点是某个类的对象,这个类可认叫做LINK.因为一个链表中有许多类似的链结点,所以有必要用一个不同于链表的类来表达链结点.每个LI ...

  6. C语言链表的转置算法,c语言编程集 数据结构 顺序表 点链表 数制转换 矩阵转置.doc...

    c语言编程集 数据结构 顺序表 点链表 数制转换 矩阵转置 #include "stdio.h" #include "malloc.h" /*typedef s ...

  7. 鸿蒙轻内核M核源码分析:数据结构之任务排序链表

    摘要:鸿蒙轻内核的任务排序链表,用于任务延迟到期/超时唤醒等业务场景,是一个非常重要.非常基础的数据结构. 本文会继续给读者介绍鸿蒙轻内核源码中重要的数据结构:任务排序链表TaskSortLinkAt ...

  8. 数据结构与算法之链表结构寻找p、q最近的公共祖先

    链表结构,寻找p.q最近的公共祖先 数据结构与算法之链表结构寻找p.q最近的公共祖先 链表结构,寻找p.q最近的公共祖先 问题 想法 代码 问题 设一棵二叉树的结点结构为(LLINK, INFO, R ...

  9. 数据结构与算法--单链表相关面试题

    此文章仅作为自己学习过程中的记录和总结,同时会有意地去用英文来做笔记,一些术语的英译不太准确,内容如有错漏也请多指教,谢谢! 一.概述 获取单链表的有效元素个数[新浪面试题1] 获取单链表倒数第k个结 ...

最新文章

  1. python详细教程-Python入门教程:超详细1小时学会Python
  2. wxWidgets:wxMenuEvent类用法
  3. 五一黄金周之二:可以吃人的博物馆
  4. 华为鸿蒙 os 适配机型曝光,华为鸿蒙OS 2.0首批适配机型曝光
  5. python基础--函数作用域
  6. 端子排延时中间继电器DZS-822/DC110V
  7. 2012考研数学二第(21)题——中值定理:零点定理+数列极限:单调有界准则
  8. 公有云Docker镜像P2P加速之路:安全篇
  9. centos7更新nvidia显卡驱动
  10. 【无标题】There was an unexpected error (type=Internal Server Error, status=500).
  11. 大数据学习之sqoop
  12. 短信业务 防恶意攻击解决方案
  13. TERMIOS详解【转】
  14. 支付宝当面付实现跳转到指定网页唤起支付
  15. USB 对拷线材 YYDS
  16. excel文件底层xml分析,如何实现高效解析excel
  17. 大学认可的一类出版社目录
  18. Android平移补间动画,Android 补间动画之平移动画TranslateAnimation
  19. 洛谷P2489 [SDOI2011]迷宫探险
  20. java前期准备之进制转换

热门文章

  1. 台式计算机cpu多好,2019台式处理器排行榜_台式机处理器排行榜 前六强详细介绍...
  2. 大疆aeb连拍_大疆 AIR2 玩机(一)包围曝光及后期处理
  3. 工业4.0围观,大炒作,大问题,大差距,你发现了吗?
  4. OmniGraffler软件和激活码
  5. 【C/C++】一个实例看 回调函数 / 函数指针 应用与原理
  6. rabbitmq python 发送失败_python rabbitmq no_ack=false
  7. android socket编程实例
  8. mysql flaskalchemy_python flask sqlalchemy 数据库mysql操作
  9. C#自动切换Windows窗口程序,如何才能调出主窗口?
  10. ubuntu10.04以及10.10安装配置tftp服务