• java数据结构与算法——链表面试题


    本文为链表相关面试题,每道题均附讲解及对应链接。

    一:反转一个单链表

    链接:206. 反转链表 - 力扣(LeetCode)https://leetcode.cn/problems/reverse-linked-list/description/

    1.题目:给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

    2.解题

             我的思路是:从第二个结点开始,依次将后一个结点的指向改变为指向前一个结点。如果直接改变指向,那么就无法找到下一个结点了,所以需要设置一个curNext,保存下一个结点。

            每次都让当前结点指向前一结点,然后head和cur都后移一个节点。直到cur == null时,说明反转结束,直接返回head结点即可。

            具体代码如下:

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. class Solution {
    12. public ListNode reverseList(ListNode head) {
    13. if(head == null){
    14. return head;
    15. }
    16. ListNode cur = head.next;
    17. ListNode curNext = new ListNode();
    18. head.next = null;
    19. while(cur != null){
    20. curNext = cur.next;
    21. cur.next = head;
    22. head = cur;
    23. cur = curNext;
    24. }
    25. return head;
    26. }
    27. }

    二:寻找链表的中间结点 

    链接:

    876. 链表的中间结点 - 力扣(LeetCode)https://leetcode.cn/problems/middle-of-the-linked-list/comments/

    1.题目:给定一个头结点为 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。

    2.解题

            我们很容易想到先求出链表长度,再找到中间的结点,但是这种做法的时间复杂度较高。面对这类题时,比较常见的做法是使用“快慢指针”。

            设置fast和slow“指针”,slow指针走一步,fast指针走两步,这样当fast走到链表末尾时,slow恰巧到达链表的中间位置。

            具体代码如下:

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. class Solution {
    12. public ListNode middleNode(ListNode head) {
    13. ListNode slow = head;
    14. ListNode fast = head;
    15. while(fast != null && fast.next != null){
    16. slow = slow.next;
    17. fast = fast.next.next;
    18. }
    19. return slow;
    20. }
    21. }

    三:输出链表中倒数第k个结点

    链接:

    链表中倒数第k个结点_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&&tqId=11167&rp=2&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

    1.题目:输入一个链表,输出该链表中倒数第k个结点。

    2.解题

            这道题任然可以沿用“快慢指针”的思路。让快指针先走k步,然后快慢指针一起走。当快指针走到链表的末尾时,慢指针所在的位置就是链表中倒数第k个结点。当然我们考虑的是最佳情况,而在解决问题时,最重要的就是要考虑到所有的情况。

            特殊情形1:k <= 0;直接返回null;

            特殊情形2:head == null;直接返回null;

            特殊情形3:在fast向后移动k个结点的过程中,fast指针为空了,说明整个链表的长度都不足k,当然不会有倒数第k个结点了,直接返回null。

    具体代码如下:

    1. public class Solution {
    2. public ListNode FindKthToTail(ListNode head,int k) {
    3. if(k <= 0){
    4. return null;
    5. }
    6. if(head == null){
    7. return head;
    8. }
    9. ListNode slow = head;
    10. ListNode fast = head;
    11. while(k-1>0){
    12. fast = fast.next;
    13. if(fast == null){
    14. return null;
    15. }
    16. k--;
    17. }
    18. while(fast.next != null){
    19. slow = slow.next;
    20. fast = fast.next;
    21. }
    22. return slow;
    23. }
    24. }

     四:合并两个有序链表

    链接:

    21. 合并两个有序链表 - 力扣(LeetCode)https://leetcode.cn/problems/merge-two-sorted-lists/description/

    1.题目:将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

     2.解题

            两个链表的头节点分别是head1,head2。从头结点开始,依次比较两个链表结点值的大小,并将较小的那个节点加入到合并链表中,head1(head2)向后移动一位,继续比较剩下的结点。直到其中一个链表的所有节点都已加入到合并链表中,此时就将另一个链表中的剩余结点接到合并链表中,程序结束。

            本题整体思路比较简单,需要考虑的特殊情况是某个头节点为空两个头结点都为空时,无需进行合并,直接返回另一个节点的头结点即可。

    具体代码如下:

    1. public static ListNode mergeTwoLists(ListNode head1, ListNode head2) {
    2. if(head1 == null){
    3. return head2;
    4. }
    5. if(head2 == null){
    6. return head1;
    7. }
    8. ListNode head = new ListNode();
    9. ListNode cur = head;
    10. while (head1 != null && head2 != null){
    11. if(head1.val < head2.val){
    12. cur.next = head1;
    13. cur = cur.next;
    14. head1 = head1.next;
    15. }else{
    16. cur.next = head2;
    17. cur = cur.next;
    18. head2 = head2.next;
    19. }
    20. }
    21. if(head2 == null){
    22. cur.next = head1;
    23. }else{
    24. cur.next = head2;
    25. }
    26. return head.next;
    27. }

    五:链表分割

    链接:

    链表分割_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/0e27e0b064de4eacac178676ef9c9d70?tpId=8&&tqId=11004&rp=2&ru=/activity/oj&qru=/ta/cracking-the-coding-interview/question-ranking

    1.题目:现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。

    2.解题 

            使用两个链表,一个链表放小于x的结点,另一个链表放大于x的结点。遍历原链表结束后,就可以将所有小于x的结点放在第一个链表中,其余结点放在第二个链表中,且顺序不变。最后,将两个链表连接起来即可。

    特殊情况及注意事项:

    1.特殊情况:当链表为空时,直接返回null;

    2.注意事项:连接两个链表后,一定要将第二个链表的next域置为null,否则会出现混乱。

    具体代码如下:

    1. import java.util.*;
    2. /*
    3. public class ListNode {
    4. int val;
    5. ListNode next = null;
    6. ListNode(int val) {
    7. this.val = val;
    8. }
    9. }*/
    10. //思路:
    11. //1.分为两个链表,一个链表放小于x的节点,一个链表放大于等于x的节点
    12. //2.连接两个链表
    13. public class Partition {
    14. public ListNode partition(ListNode pHead, int x) {
    15. // write code here
    16. ListNode head1 = null;
    17. ListNode last1 = null;
    18. ListNode head2 = null;
    19. ListNode last2 = null;
    20. if(pHead == null){
    21. return null;
    22. }
    23. while(pHead != null){
    24. if(pHead.val < x){
    25. if(head1 == null){
    26. head1 = pHead;
    27. last1 = pHead;
    28. }else{
    29. last1.next = pHead;
    30. last1 = last1.next;
    31. }
    32. }
    33. else{
    34. if(head2 == null){
    35. head2 = pHead;
    36. last2 = pHead;
    37. }
    38. else{
    39. last2.next = pHead;
    40. last2 = last2.next;
    41. }
    42. }
    43. pHead = pHead.next;
    44. }
    45. //进行连接
    46. if(head2 == null){
    47. return head1;
    48. }
    49. if(head1 == null){
    50. return head2;
    51. }
    52. last1.next = head2;
    53. last2.next = null;//最后一个next域置为null
    54. return head1;
    55. }
    56. }

    六:链表的回文结构

    链接:

    链表的回文结构_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa?tpId=49&&tqId=29370&rp=1&ru=/activity/oj&qru=/ta/2016test/question-ranking

    1.题目:

    对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。

    2.解题

            回文链表类似于这样的结构:1-3-4-4-3-1,显然从中间分开,两边对称。结合之前讲到的链表反转和寻找中间结点,我们考虑先找到中间结点,然后从中间结点开始,将其后的链表反转;然后遍历两个链表,依次比较每一个结点的值。如果其中两个结点的值不同,说明这不是一个回文串,返回false即可。图解如下:

             显然结点个数不同,判断结束的标志也有所差异,我们只需将这两种情况都添加到我们的判断语句中即可,当head == slow || head.next == slow时,表明每个节点都已经判断完毕,该链表是回文的,返回true。

    具体代码如下:

    1. import java.util.*;
    2. class ListNode {
    3. int val;
    4. ListNode next = null;
    5. ListNode(int val) {
    6. this.val = val;
    7. }
    8. }
    9. public class PalindromeList {
    10. public boolean chkPalindrome(ListNode A) {
    11. //1.找到中间结点
    12. ListNode head = A;
    13. ListNode slow = head;
    14. ListNode fast = head;
    15. while(fast != null && fast.next != null){
    16. slow = slow.next;
    17. fast = fast.next.next;
    18. }
    19. //此时,slow已经指向中间的结点
    20. //2.从中间结点开始进行反转
    21. ListNode cur = slow.next;
    22. ListNode curNext = new ListNode(1);
    23. while(cur != null) {
    24. curNext = cur.next;
    25. cur.next = slow;
    26. slow = cur;
    27. cur = curNext;
    28. }
    29. //反转成功,最开始有个head,最后面有个slow
    30. //3.从两边,向中间走
    31. while(head.val == slow.val){
    32. head = head.next;
    33. slow = slow.next;
    34. if(head == slow || head.next == slow){
    35. return true;
    36. }
    37. }
    38. return false;
    39. }
    40. }

    七:判断链表中是否有环

    链接:

    141. 环形链表 - 力扣(LeetCode)https://leetcode.cn/problems/linked-list-cycle/description/

    1.题目:给你一个链表的头节点 head ,判断链表中是否有环。如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 

    2.解题

            快慢指针。慢指针一次走一步,快指针一次走两步,如果链表中有环,快慢指针一定会相遇。

            具体代码如下:

    1. /**
    2. * Definition for singly-linked list.
    3. * class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode(int x) {
    7. * val = x;
    8. * next = null;
    9. * }
    10. * }
    11. */
    12. public class Solution {
    13. public boolean hasCycle(ListNode head) {
    14. ListNode slow = head;
    15. ListNode fast = head;
    16. while(fast!=null && fast.next!= null){
    17. slow = slow.next;
    18. fast = fast.next.next;
    19. if(slow == fast){
    20. return true;
    21. }
    22. }
    23. return false;
    24. }
    25. }

    八:环形链表

    链接:

    142. 环形链表 II - 力扣(LeetCode)https://leetcode.cn/problems/linked-list-cycle-ii/description/

    1.题目:给定一个链表的头节点  head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

    2.解题:

            这是一个简单的数学问题。

     

     具体代码如下:

    1. /**
    2. * Definition for singly-linked list.
    3. * class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode(int x) {
    7. * val = x;
    8. * next = null;
    9. * }
    10. * }
    11. */
    12. public class Solution {
    13. public ListNode detectCycle(ListNode head) {
    14. ListNode slow = head;
    15. ListNode fast = head;
    16. if(slow == null || slow.next == null) {
    17. return null;
    18. }
    19. ListNode k = head;
    20. while(fast!=null && fast.next!= null){
    21. slow = slow.next;
    22. fast = fast.next.next;
    23. if(slow == fast){
    24. break;
    25. }
    26. }
    27. while(slow != null ){
    28. if(slow == k){
    29. return slow;
    30. }
    31. k = k.next;
    32. slow = slow.next;
    33. }
    34. return null;
    35. }
    36. }

            以上就是关于链表的一些习题。“快慢指针”在解决链表问题时经常出现,我们一定要掌握好这种方法;其次,在解决题目六:链表的回文结构时,我们用到了“快慢指针”和“反转链表”的相关知识,很容易就解决了这个题目,这说明再解决一些较为复杂的问题时,可以将其拆分为一个个简单问题加以解决。

    九 : 相交链表

    链接:160. 相交链表 - 力扣(LeetCode)https://leetcode.cn/problems/intersection-of-two-linked-lists/

    1.题目:给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。

    2.解题

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode(int x) {
    7. * val = x;
    8. * next = null;
    9. * }
    10. * }
    11. */
    12. public class Solution {
    13. public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    14. if (headA == null || headB == null) return null;
    15. ListNode pA = headA, pB = headB;
    16. while (pA != pB) {
    17. pA = (pA == null) ? headB : pA.next;
    18. pB = (pB == null) ? headA : pB.next;
    19. }
    20. return pA;
    21. }
    22. }

    注意 : 运算符优先级问题 : == 高于 ?= 高于 = .

    优先级运算符结合性
    1()、[]、{}从左向右
    2!、+、-、~、++、--从右向左
    3*、/、%从左向右
    4+、-从左向右
    5«、»、>>>从左向右
    6<、<=、>、>=、instanceof从左向右
    7==、!=从左向右
    8&从左向右
    9^从左向右
    10|从左向右
    11&&从左向右
    12||从左向右
    13?:从右向左
    14=、+=、-=、*=、/=、&=、|=、^=、~=、«=、»=、>>>=从右向左

            链表题目浩如烟海,以上所展示的不过是沧海一粟罢了。笔者将力扣及牛客网上链表部分的链接放在下面,感兴趣的朋友可以自行选择练习。

    链接:

    链表知识点题库 - 力扣(LeetCode)https://leetcode.cn/tag/linked-list/problemset/链表知识点题库 - 牛客网(nowCoder)https://www.nowcoder.com/exam/oj

    本课内容结束!

  • 相关阅读:
    经典算法试题(一)
    0:node的安装与环境配置
    ES聚合统计
    【推荐】如何使用drawio的编辑器,看本文就够
    软件依赖管理-源码依赖、接口依赖、服务依赖
    QT信号和槽的关联实现子窗口传递值给主窗口
    跨境电商与支付介绍
    体验 Shifu 解决报错流程
    树莓派4b开机自启sh脚本
    dll动态链接库及ocx activex 控件regsvr32注册失败 解决方法(Win10)
  • 原文地址:https://blog.csdn.net/baijaiyu/article/details/124849386