无头单向非循环链表实现 and leetcode刷题


写在最前面,学习数据结构一定要结合画图!先画图分析,写出伪代码,再仔细分析伪代码是否成立,成立再写入题目中检验!

1. 单链表的模拟实现

单链表的模拟实现需要创建三个文件:IList.java接口文件,MySingleList.java文件,还有一个test.java测试文件。测试文件这里就不演示了。

IList.java接口:

public interface IList {
    // 1、无头单向非循环链表实现
        //头插法
        void addFirst(int data);
        //尾插法
        void addLast(int data);
        //任意位置插入,第一个数据节点为0号下标
        void addIndex(int index,int data);
        //查找是否包含关键字key是否在单链表当中
        boolean contains(int key);
        //删除第一次出现关键字为key的节点
        void remove(int key);
        //删除所有值为key的节点
        void removeAllKey(int key);
        //得到单链表的长度
        int size();
        void clear();
        void display();
}

MySingleList.java文件:

public class MySingleList implements IList{
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;

    public void creatList() {
        ListNode node1 = new ListNode(0);
        ListNode node2 = new ListNode(1);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(3);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }
        //找尾
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    @Override
    public void addIndex(int index, int data) {
        if(index < 0 || index > size()) {
            System.out.println("index位置不合法!");
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = head;
        for (int i = 0; i < index-1; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode pre = head;
        while(pre.next != null) {
            if(pre.next.val == key) {
                ListNode del = pre.next;
                pre.next =del.next;
                return;
            }
            pre = pre.next;
        }
    }


    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                pre.next = cur.next;
            }else {
                pre = cur;
            }
            cur = cur.next;
        }
        //该if语句只能放在最后面,如果头节点需要删除,
        //删除后有可能下一个节点(此时这个节点做头节点)依然是需要删除的
        //因此,只能放在最后,当后面的都删除好了,再检查头节点是否需要删除
        if(head.val == key) {
            head = head.next;
        }
    }

    @Override
    public int size() {
        int len = 0;
        ListNode cur = head;
        while(cur != null) {
            cur = cur.next;
            len++;
        }
        return len;
    }

    @Override
    public void clear() {
        head = null;
    }
}

2. leetcode刷题

2.1 获取链表的中间节点

题目链接:876. 链表的中间结点

注意:题目中说明当链表只有一个中间结点时,返回该节点;而当该链表有两个中间结点,返回第二个结点

解析:定义一对“快慢指针”,“快指针”为fast,一次走两步;“慢指针”为slow,一次走一步。

  • 当链表的结点个数为奇数个时,fast走到fast.next == null时,slow此时所在位置就是中间节点
  • 当链表的节点个数为偶数个时,fast走到fast == null时,slow此时所在位置就是中间节点

代码如下:

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

2.2 删除链表中所有值为value的元素

题目链接:203. 移除链表元素

这题的题解和模拟实现单链表的removeAllKey是一样的,故不再赘述。

代码如下:

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) {
            return head;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == val) {
                pre.next = cur.next;
            }else {
                pre = cur;
            }
            cur = cur.next;
        }
        if(head.val == val) {
            head = head.next;
        }
        return head;
    }
}

2.3 单链表的逆置

题目链接:206. 反转链表

解析:只需将链表的每个箭头调转方向即可,即修改当前节点的next值为前一个节点的地址,修改后就无法获取下一个节点了,故需要一个curN来定位下一个节点,又由于是单链表,无法得到前一个节点的位置,所以还需要定义一个prev来定位前一个节点的位置

在这里插入图片描述
代码如下:

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        ListNode curN = head.next.next;
        pre.next = null;
        while(cur != null) {
            cur.next = pre;
            pre = cur;
            cur = curN;
            if(curN != null) {
                curN = curN.next;
            }
        }
        return pre;
    }
}

2.4 获取链表倒数第k个节点

题目链接:面试题 02.02. 返回倒数第 k 个节点

解析:定义一对“快慢指针”,”快指针“fast先走k步,然后”快指针“fast和”慢指针“slow一起一次走一步,直至fast == null结束,这时slow指向的便是倒数第k个节点

代码如下:

class Solution {
    public int kthToLast(ListNode head, int k) {
        if(head == null) {
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;
        for(int i = 0; i < k; i++) {
            fast = fast.next;
        }
        while(fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }
}

2.5 给定 x, 把一个链表整理成前半部分小于 x, 后半部分大于等于 x 的形式

题目链接:CM11 链表分割

注意:这题是将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序

public class Partition {
    public ListNode partition(ListNode head, int x) {
        // write code here
        if(head == null) {
            return null;
        }
        ListNode bs = null;//bs:beforestart
        ListNode be = null;//be:beforeend
        ListNode as = null;//as:afterstart
        ListNode ae = null;//ae:afterend
        ListNode cur = head;
        while(cur != null) {
            if(cur.val < x) {
                if(bs == null) {//找到bs和be的起始位置
                    bs = be = cur;
                }else {
                    be.next = cur;
                    be = cur;
                }
            }else {
                if(as == null) {//找到as和ae的起始位置
                    as = ae = cur;
                }else {
                    ae.next = cur;
                    ae = cur;
                }
            }
            cur = cur.next;
        }
        //ae的next需要手动置为null
        if(ae != null) {
            ae.next = null;
        }
        //如果链表的节点都大于x,则返回as
        if(bs == null) {
            return as;
        }
        //bs不为null,be自然也不为空
        be.next = as;
        return bs;
    }
}

2.6 判定链表是否是回文

题目链接:OR36 链表的回文结构

public class PalindromeList {
    public boolean chkPalindrome(ListNode A) {
        // write code here
        if(A == null) {
            return false;
        }
        if(A.next == null) {
            return true;
        }
        //1.找到中间节点
        ListNode mid = getMiddleNode(A);
        //2.反转后半部分
        ListNode as = reseverList(mid);
        mid.next = null;//一定要置null!
        //3.从前往后依次对比两个链表的val值是否相同
        ListNode bs = A;
        while(bs.next != null && as.next != null) {
            if(bs.val != as.val) {
                return false;
            }
            bs = bs.next;
            as = as.next;
        }
        if(bs.val != as.val) {
            return false;
        }
        return true;
    }
    private ListNode reseverList(ListNode head) {
        ListNode prev = head;
        ListNode cur = head.next;
        ListNode curN = cur.next;
        while(cur != null) {
            cur.next = prev;
            prev = cur;
            cur = curN;
            if(curN != null){
                curN = curN.next;
            }
        }
        return prev;
    }
    private ListNode getMiddleNode(ListNode A) {
        ListNode fast = A;
        ListNode slow = A;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

2.7 判定链表相交并求出交点

题目链接:160. 相交链表

解题思路:

  1. 分别求出两个链表的长度,并得到两链表的长度差值(正数)
  2. 先让长链表的“l指针”走长度差值步,再让“l指针”和“s指针”一起走,如果相遇,相遇点即为相交链表的交点,如果没有相遇,则最后l和s同时为null
  3. 检验当两个链表同时为null时,代码是否满足

代码如下:

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int lenA = size(headA);
        int lenB = size(headB);
         //先假设headA链表的长度大于headB链表
        ListNode l = headA;
        ListNode s = headB;
        int len = lenA-lenB;
        //如果是headB链表更长,则进入if语句,进行调换
        if(len < 0) {
            len = -len;
            l = headB;
            s = headA;
        }
        for(int i = 0; i < len; i++) {
            l = l.next;
        }
        while(l != s) {
            l = l.next;
            s = s.next;
        }
        if(l == null) {
            return null;//没相交
        }
        return l;
    }
    public int size(ListNode head) {
        int len = 0;
        ListNode cur = head;
        while(cur != null) {
            cur = cur.next;
            len++;
        }
        return len;
    }
}

2.8 判断链表带环

题目链接:141. 环形链表

解题思路:

  1. 定义一对“快慢指针”,快指针fast一次走两步,慢指针slow一次走一步
  2. 如果最后fast == slow,则说明该链表存在环形结构;如果最后 fast == null || fast.next == null,则说明该链表不存在环形结构
  3. 检验链表为null时,代码是否满足

代码如下:

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;
    }
}

2.9 求环的入口点

题目链接:142. 环形链表 II

解题思路:

  1. 先判断链表结构中是否存在环(在2.8代码中进行略微修改即可)
  2. 求交点: 让一个引用从链表起始位置开始,一个引用从相遇点位置开始,两个引用每次都走一步,最终相遇时的节点即为交点(原因如下)

数学推导:
在这里插入图片描述
代码如下:

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            //这个if语句必须放在下面,否则该if语句第一次就会成立,
            //因为fast和slow第一次都是head
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;
        }
        slow = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}

2.10 合并两个有序链表

题目链接:21. 合并两个有序链表

解题思路:

  1. 创建一个带头结点的单链表
  2. 依次对比两个链表的数值大小,小的尾插到新链表尾部
  3. 当一个链表被新链表连接完时,另一个链表剩下的部分直接尾插到新链表的尾部
  4. 检验当一个链表为null时,代码是否满足

代码如下:

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode cur1 = list1;
        ListNode cur2 = list2;
        ListNode newHead = new ListNode();//NewHead为带头结点
        ListNode curN = newHead;
        while(cur1 != null && cur2 != null) {
            if(cur1.val < cur2.val) {
                curN.next = cur1;
                cur1 = cur1.next;
            } else {
                curN.next = cur2;
                cur2 = cur2.next;
            }
            curN = curN.next;
        }
        if(cur1 == null) {
            curN.next = cur2;
        }
        if(cur2 == null) {
            curN.next = cur1;
        }
        return newHead.next;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值