数据结构之链表

目录

一、顺序表的缺点

二、链表

1、特点

2、存在的几种链式结构

3、LinkedList

(1)底层原理

(2)可操作方法

4、常见面试题

(1)删除链表中等于给定值 val 的所有节点。

(2)反转一个单链表。

(3)返回链表的中间结点

(4)输出该链表中倒数第k个结点

(5)两个有序链表合并为一个新的有序链表并返回

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

(7)链表的回文结构

(8)输入两个链表,找出它们的第一个公共结点

(9)给定一个链表,判断链表中是否有环

(10)给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL

三、顺序表与链表区别


一、顺序表的缺点

在顺序表中任意位置插入或删除元素时,需要把元素整体往后或往前移动,时间复杂度为O(n)。因此顺序表不适用于任意位置插入或删除元素的场景。

二、链表

1、特点

在逻辑上是连续的,但在物理上不一定是连续的。

2、存在的几种链式结构

(1)单向

(2)双向

(3)无头

(4)有头

(5)不循环

(6)循环

3、LinkedList
(1)底层原理

是无头双向循环链表。

(2)可操作方法

4、常见面试题
(1)删除链表中等于给定值 val 的所有节点。
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)反转一个单链表。
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode cur=head.next;
        head.next=null;
        while(cur!=null){
            ListNode curnext=cur.next;
            cur.next=head;
            head=cur;
            cur=curnext;
        }
        return head;
    }
}
(3)返回链表的中间结点
class Solution {
    public ListNode middleNode(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode slow=head;
        ListNode fast=head;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        return slow;
    }
}
(4)输出该链表中倒数第k个结点
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head==null) return null;
        ListNode cur=head;
        int size=0;
        while(cur!=null){
            size++;
            cur=cur.next;
        }
        if(k<=0||k>size){
            return null;
        }
        ListNode fast=head;
        ListNode slow=head;
        while(k-1!=0){
            fast=fast.next;
            k--;
        }
        while(fast.next!=null){
            slow=slow.next;
            fast=fast.next;
        }
        return slow;
    }
}
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(k<=0||head==null){
            return null;
        }
        ListNode fast=head;
        ListNode slow=head;
        int count=0; 
//要么走k-1步结束循环,要么长度不够长退出循环
        while(k-1!=count){
            fast=fast.next;
            if(fast==null){
                return null;
            }
            count++; 
        }
        while(fast.next!=null){
            slow=slow.next;
            fast=fast.next;
        }
        return slow;
    }
}
(5)两个有序链表合并为一个新的有序链表并返回
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1==null) return list2;
        if(list2==null) return list1;
        ListNode head1=new ListNode(0);
        ListNode head2=head1;
        while(list1!=null&&list2!=null){
            if(list1.val<list2.val){
                head2.next=list1;
                head2=list1;
                list1=list1.next;
            }else{
                head2.next=list2;
                head2=list2;
                list2=list2.next;
            }
        }
        if(list1==null){
            head2.next=list2;
        }else{
            head2.next=list1;
        }
        return head1.next;
    }
}
(6)以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        ListNode head1=null;
        ListNode cur1=null;
        ListNode cur2=null;
        ListNode head2=null;
        while(pHead!=null){
            if(pHead.val<x){
                if(head1==null){
                    head1=new ListNode(pHead.val);
                    cur1=head1;
                }else{
                    cur1.next=pHead;
                    cur1=pHead;
                }
            }else{
                if(head2==null){
                    head2=new ListNode(pHead.val);
                    cur2=head2;
                }else{
                    cur2.next=pHead;
                    cur2=pHead;
                }
            }
            pHead=pHead.next;
        }
        if(head2==null){
            return head1;
        }else if(head1==null){
            return head2;
        }else{
            cur2.next=null;
            cur1.next=head2;
            return head1;
        }
    }
}
(7)链表的回文结构
public class PalindromeList {
    public boolean chkPalindrome(ListNode A) {
        // write code here
        if(A==null||A.next==null) return true;
        ListNode slow=A;
        ListNode fast=A;
        ListNode B=null;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        if(fast==null){
            B=slow;
        }else{
            B=slow.next;
        }
        ListNode C=reverseList(B);
        while(C!=null){
            if(C.val!=A.val){
                return false;
            }
            C=C.next;
            A=A.next;
        }
        return true;
    }
    public ListNode reverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode cur=head.next;
        head.next=null;
        while(cur!=null){
            ListNode curnext=cur.next;
            cur.next=head;
            head=cur;
            cur=curnext;
        }
        return head;
    }
}
(8)输入两个链表,找出它们的第一个公共结点
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null&&headB!=null) return null;
        if(headA!=null&&headB==null) return null; 
        ListNode cur1=headA;
        ListNode cur2=headB;
        int size1=size(cur1);
        int size2=size(cur2);
        int difference=0;
        if(size1>size2){
            difference=size1-size2;
            while(difference!=0){
                cur1=cur1.next;
                difference--;
            }
        }else{
            difference=size2-size1;
            while(difference!=0){
                cur2=cur2.next;
                difference--;
            }
        }
        while(cur1!=null){
            if(cur1==cur2){
                return cur1;
            }else{
                cur1=cur1.next;
                cur2=cur2.next;
            }
        }
        return null;
    }
    public int size(ListNode cur){
        int count=0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
}
(9)给定一个链表,判断链表中是否有环
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow)
            return true;
        }
        return false;
    }
}

(10)给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head==null||head.next==null){
            return null;
        }
        ListNode cur=head;
        //先找到相遇点
        ListNode slow=head;
        ListNode fast=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(slow==fast) break;
        }
        if(fast==null||fast.next==null) return null;
        ListNode cur1=slow;
        //一个从相遇点出发,一个从起点出发,再次相遇时,相遇点为环的起点
        while(cur!=cur1){
            cur=cur.next;
            cur1=cur1.next;
        }
        return cur;
    }
}

nR-X=(n-1)R+R-x; L大,环小,多走几圈。

三、顺序表与链表区别

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ambition…

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

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

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

打赏作者

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

抵扣说明:

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

余额充值