【数据结构】链表面试题总结(持续更新中...)

目录

问题1:删除链表中等于给定值 val 的所有节点

问题2:反转一个单链表

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

问题4:输入一个链表,输出该链表中倒数第k个结点

问题5:合并两个有序列表

问题6:以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前

问题7:请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。

问题8:链表的回文结构

问题9:输入两个链表,找出它们的第一个公共结点

问题10:判断链表中是否有环

问题11:给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null


问题1:删除链表中等于给定值 val 的所有节点

OJ链接:203. 移除链表元素 - 力扣(LeetCode)

问题描述:

 解题思路:

要点1:先不管第一个节点,从第二个节点开始。

要点2:需要保存待删除节点的前驱节点

要点3:如果满足删除条件,pre.next = cur.next

要点4:最后判断第一个节点是否需要删除

实现代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return null;
        }
        // 先不管第一个节点,从第二个节点开始判断。
        // 需要保存待删除节点的前驱节点
        ListNode pre = head;
        ListNode cur = pre.next;
        while(cur != null){
            if(cur.val == val){
                // 如果相等,直接删除
                pre.next = cur.next;
                cur = cur.next;
            }else{
                // 一起向后移动
                pre = cur;
                cur = cur.next;
            }
        }
        // 判断头节点是否需要删除
        if(head.val == val){
            head = head.next;
        }
        return head;

    }
}

问题2:反转一个单链表

OJ链接:206. 反转链表 - 力扣(LeetCode)

问题描述:

  解题思路:

要点1:采用头插法

要点2:定义傀儡节点为null,这个节点的初值是反转链表的尾巴。定义为null最合适

要点3:依次将每个节点插在newHead前面。每插一次,更新newHead的位置

实现代码:

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null){
            return null;
        }
        // 定义一个傀儡节点
        ListNode newHead = null;
        ListNode cur = head;
        // 依次将每个节点使用头插法 插在newHead的前面
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = newHead;
            newHead = cur;
            cur = curNext;
        }
        return newHead;
    }
        
}

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

OJ链接:876. 链表的中间结点 - 力扣(LeetCode)

问题描述:

 解题思路:

要点1:快慢节点。第一个节点的速度是第二个速度的二倍。

原因:假设一段路为L,当A的速度是B的速度的两倍,同样的时间,当A跑完全程,B就会跑动中点

要点2:遍历链表的条件是:fast != null && fast.next != null

原因:因为fast要连走两步,如果fast.next=null,走第二步就会报空指针异常

实现代码:

class Solution {
    public ListNode middleNode(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

问题4:输入一个链表,输出该链表中倒数第k个结点

OJ链接:链表中倒数第k个结点_牛客题霸_牛客网 (nowcoder.com)

问题描述:

解题思路:

要点1:让快的节点先走k-1步。

要点2:k要是为0,直接返回null

要点3:k如果大于链表的长度,直接返回null。在fast走K步的过程中,判断fast=null

要点4:fast先走K步后,fast和slow的速度一样

要点5:fast走到最后一个节点时,返回slow

实现代码:

public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head == null){
            return null;
        }
        // 返回倒数第K个,快比慢多走K-1步
        ListNode fast = head;
        ListNode slow = head;
        if(k == 0){
            return null;
        }
        // 先让快的先走
        while(k-1 > 0){
            fast = fast.next;
            if(fast == null){
                // 证明K大于链表个数
                return null;
            }
            k--;
        }
// fast走到最后一个节点,就停止,当fast.next =null,就不进入这个while
        while(fast != null && fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;

    }
}

问题5:合并两个有序列表

OJ链接:21. 合并两个有序链表 - 力扣(LeetCode)

问题描述:

 解题思路:

要点1:定义一个傀儡节点,将小的节点往这个傀儡节点后面加

要点2:两个链表的长度不一定相等,跳出 while(cur1 != null && cur2 != null),至少有一个链表已经遍历介绍,将还没遍历完的链表的剩余节点加载newCur的后面

实现代码:

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null){
            return list2;
        }
        if(list2 == null){
            return list1;
        }
        ListNode newHead = new ListNode(-1);
        ListNode cur1 = list1;
        ListNode cur2 = list2;
        // 定义一个傀儡节点,把小的节点往这个傀儡节点后面添加,每次添加后,更新newCur的位置
        ListNode newCur = newHead;
        while(cur1 != null && cur2 != null){
            if(cur1.val >= cur2.val){
                newCur.next = cur2;
                cur2 = cur2.next;
                newCur = newCur.next;
            }else{
                newCur.next = cur1;
                cur1 = cur1.next;
                newCur = newCur.next;
            }
        }
        // 因为两个链表的长度不一定相等,经过上面的while,至少有一个链表已经遍历结束了
        // 将不为空的链表加载newCur的后面
        if(cur1 != null){
            newCur.next = cur1;
        }
        if(cur2 != null){
            newCur.next = cur2;
        }
        return newHead.next;
    }
}

问题6:以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前

OJ链接:链表分割_牛客题霸_牛客网 (nowcoder.com)

问题描述:

解题思路:

要点1:定义两个链表。第一个链表存放比x小的值,第二个链表存放比x大的值。然后将这两个链表串在一起

要点2:使用尾插法

要点3:注意注意,第一个和第二个都有可能为空。当第二段不为空,需要修改链表末尾位置的指向

要点4:cur.val < x,不能写小于等于。如果加上等于,就会出现比x小的值在x的后面这种情况

实现代码:

public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        if(pHead == null){
            return null;
        }
        // 用来存放比x小的元素(有可能为空)
        ListNode s1 = null;
        ListNode e1 = null;
        // 用来存放比x大的元素(有可能为空)
        ListNode s2 = null;
        ListNode e2 = null;
        // 需要对新链表的尾节点处理
        ListNode cur = pHead;
        while(cur != null){
            if(cur.val < x){
                if(s1 == null){
                    // 第一次插入,s1和e1都指向该元素
                    s1 = cur;
                    e1 = cur;
                }else{
                    e1.next = cur;
                    e1 = e1.next;
                }
            }else{
                if(s2 == null){
                    s2 = cur;
                    e2 = cur;
                }else{
                    e2.next = cur;
                    e2 = e2.next;
                }
            }
            cur = cur.next;
        }
        if(s1 == null){
            return s2;
        }
        // 拼接两个链表
        e1.next = s2;
        
        if(s2 != null){
            e2.next = null;
        }
        return s1;
        
    }
}

问题7:请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。

OJ链接:删除链表中重复的结点_牛客题霸_牛客网 (nowcoder.com)

问题描述:

 

 解题思路:

要点1:定义一个傀儡节点。如果相邻元素不相同,就采用尾插法插到傀儡节点的后面。如果相等。就让cur往后移,跳过所有相等的点。让tmp.next = cur;

实现代码:

public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        if(pHead == null || pHead.next == null){
            return pHead;
        }
        ListNode newHead = new ListNode(-1);
        ListNode cur = pHead;
//         ListNode curNext = null;
        ListNode tmp = newHead;
        while(cur != null){
            if(cur.next != null && cur.val == cur.next.val){
                while(cur.next != null && cur.val == cur.next.val){
                    cur = cur.next;
                }
// 移到相等的下一个节点
                cur = cur.next;
            }else{
                tmp.next = cur;
                tmp = tmp.next;
                cur = cur.next;
            }
        }
        tmp.next = null;
        return newHead.next;

    }
}

问题8:链表的回文结构

OJ链接:链表的回文结构_牛客题霸_牛客网 (nowcoder.com)

问题描述:

 解题思路:

要点1:找到中间的节点,中间节点之后的节点挨个反转。

要点2:如果节点是奇数个,循环结束的条件是head1和head2相遇

要点3:如果节点是奇数个,循环结束的条件是head1.next = head2

实现代码:

public class PalindromeList {
    public boolean chkPalindrome(ListNode A) {
        if(A == null){
            return false;
        }
        if(A.next == null){
            return true;
        }
        
        ListNode fast = A;
        ListNode slow = A;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        // 此时slow就是中间节点,从slow开始翻转
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curNext = cur.next;
            // 头插法,将cur插在slow前面
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        // 从两头开始遍历,判断是否相等
        ListNode head1 = A;
        ListNode head2 = slow;
        // head1.next == head2,证明是偶数个节点,并且相遇,返回true
        while(head1 != head2 && head1.next != head2){
            if(head1.val != head2.val){
                return false;
            }
            head1 = head1.next;
            head2 = head2.next;
        }
        return true;
    }
}

问题9:输入两个链表,找出它们的第一个公共结点

OJ链接:160. 相交链表 - 力扣(LeetCode)

问题描述:

 解题思路:

要点1:先求两个链表的长度,长度的差值就是长的链表需要先走的步数。

起点不一样,终点一样,速度一样,要想相遇,就得让距离远的先走

要点2:约定:cur1永远指向长的链表的起点,cur2永远指向短的链表的起点,len永远是正数。

要点3:由于求长度的时候cur1和cur2已经为null,需要重新对这两个赋值

要点4:这两个链表有可能不相交,如果其中一个cur已经为null了.还没有相等,就证明不相交

实现代码:

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null){
            return null;
        }
        if(headB == null){
            return null;
        }
        int lenA = 0;
        int lenB = 0;
        ListNode cur1 = headA;
        ListNode cur2 = headB;
        while(cur1 != null){
            cur1 = cur1.next;
            lenA++;
        }
        while(cur2 != null){
            cur2 = cur2.next;
            lenB++;
        }
        int len = lenA - lenB; 
        // 约定cur1指向长的链表  cur2指向短的链表  len永远是正数
        if(len < 0){
            cur1 = headB;
            cur2 = headA;
            len = lenB - lenA;
        }else{
            // 在求长度的时候,cur1和cur2已经走到了两个链表的末尾,需要重新赋值
            cur1 = headA;
            cur2 = headB;
        }
        // 让长的先走
        while(len >0 ){
            cur1 = cur1.next;
            len--;
        }
        while(cur1 != cur2){
            // 如果cur1走到终点,还没有相等,证明不相交
            if(cur1 == null){
                return null;
            }
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }
}

问题10:判断链表中是否有环

OJ链接:141. 环形链表 - 力扣(LeetCode)

问题描述:

 

 解题思路:

要点1:快慢指针。fast是slow速度的二部,如果是个圆,则两个一定会相遇。

实现代码:

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null){
            return false;
        }
        if(head.next == null){
            return false;
        }
        ListNode faster = head;
        ListNode slow = head;
        while(faster != null && faster.next != null && slow != null){
            faster = faster.next.next;
            slow = slow.next;
            if(faster == slow){
                return true;
            }
        }
        return false;
    }
}

问题11:给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null

OJ链接:142. 环形链表 II - 力扣(LeetCode)

问题描述:

  解题思路:

要点1:先使用快慢指针判断是否有环

要点2:h=L,让slow回到起点,fast不动。因为slow和fast距离入环点的距离一样,以相同的速度移动,相遇点就是入环点

要点3:如果L为0.当slow走一圈,fast走两圈。会在head相遇。所以如果slow=fast=head,证明head就是入环点。

 

实现代码:

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null && slow != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
         // 便秒已经遍历完了,还没有相交,证明链表没有环
        if(fast == null || fast.next == null){
            return null;
        }
        // 在这种情况下,整个链表是一个完整的圈,fast是slow速度的二倍,fast走两圈,slow走一圈,才能相遇,只能在起点相遇
        if(slow == head && fast == head){
            return head;
        }
         slow = head;
         // 
         while(slow != fast){
             slow = slow.next;
             fast = fast.next;
         }
         return slow;
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘减减

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值