代码随想录链表打卡

第三第四次补一起

学习内容

  • 代码随想录链表部分

题目

  1. 移除链表元素
  • 引入的技巧:
    • 添加dummy哨兵节点
  • 代码:
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) {
            return null;
        }
        ListNode dummy = new ListNode(-1, head);
        ListNode cur = dummy;
        while(cur.next != null) {//这里是细节
            if(cur.next.val == val) {
                ListNode temp = cur.next.next;
                cur.next = temp;
            }else {
                cur = cur.next;
            }
        }
        return dummy.next;
    }
}
  1. 设计链表
  • 纯模拟,数据结构设计
  • 代码:
class ListNode {
    int val;
    ListNode next;
    ListNode(){}
    ListNode(int val) {
        this.val=val;
    }
}
class MyLinkedList {
    //size存储链表元素的个数
    int size;
    //虚拟头结点
    ListNode head;

    //初始化链表
    public MyLinkedList() {
        size = 0;
        head = new ListNode(0);
    }

    //获取第index个节点的数值,注意index是从0开始的,第0个节点就是头结点
    public int get(int index) {
        //如果index非法,返回-1
        if (index >= size) {
            return -1;
        }
        ListNode currentNode = head;
        //包含一个虚拟头节点,所以查找第 index+1 个节点
        for (int i = 0; i <= index; i++) {
            currentNode = currentNode.next;
        }
        return currentNode.val;
    }

    //在链表最前面插入一个节点,等价于在第0个元素前添加
    public void addAtHead(int val) {
        addAtIndex(0, val);
    }

    //在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加
    public void addAtTail(int val) {
        addAtIndex(size, val);
    }

    // 在第 index 个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
    // 如果 index 等于链表的长度,则说明是新插入的节点为链表的尾结点
    // 如果 index 大于链表的长度,则返回空
    public void addAtIndex(int index, int val) {
        if (index > size) {
            return;
        }

        
        //找到要插入节点的前驱
        ListNode pred = head;
        for (int i = 0; i < index; i++) {
            pred = pred.next;
        }
        ListNode toAdd = new ListNode(val);
        ListNode t = pred.next;

        toAdd.next = t;
        pred.next = toAdd;
        size++;
    }

    //删除第index个节点
    public void deleteAtIndex(int index) {
        if (index >= size) {
            return;
        }
        size--;
        if (index == 0) {
            head = head.next;
	        return;
        }
        ListNode pred = head;
        for (int i = 0; i < index ; i++) {
            pred = pred.next;
        }
        pred.next = pred.next.next;
    }
}
  1. 反转链表
1. 反转链表
  • 太熟悉了
  • 直接上代码:
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;

        while(cur != null) {
            ListNode temp = cur.next;
            cur.next = pre;

            pre = cur;
            cur = temp;
        }
        return pre;
    }
}
2. 反转链表2
  • 需要定位到反转目标段的前一个节点,因此需要添加哨兵节点
  • 代码:
class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummy = new ListNode(0, head), p0 = dummy;
        for (int i = 0; i < left - 1; ++i)
            p0 = p0.next;

        ListNode pre = null, cur = p0.next;
        ListNode p1 = p0.next;
        for (int i = 0; i < right - left + 1; ++i) {
            ListNode nxt = cur.next;
            cur.next = pre; 
            pre = cur;
            cur = nxt;
        }

        p1.next = cur;
        p0.next = pre;
        return dummy.next;
    }
}
3. K个一组反转链表
  • 核心就是要记录节点,并且每次循环都要更新dummy
  • 代码:
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode dummy = new ListNode(0, head);
        ListNode p0 = dummy;
        int L = 0;
        while(p0 != null) {
            p0 = p0.next;
            L++;
        }
        L = L - 1;
        
        p0 = dummy;

        for(int i = L; i >= k; i-=k) {
            ListNode pre = null;
            ListNode p1 = p0.next;
            ListNode cur = p0.next;
            for(int j = 0; j < k; j++) {
                ListNode temp = cur.next;
                cur.next = pre;

                pre = cur;
                cur = temp;
            }
            p1.next = cur;
            p0.next = pre;
            p0 = p1;
        }
        return dummy.next;
    }
}
  1. 删除链表倒数第N个节点
  • 核心就是添加dummy,定位到前一个节点
  1. 环形链表1
  • 快慢指针
  • 代码:
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;

        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast) {
                return true;
            }
        }
        return false;
    }
}
  1. 环形链表2
  • 核心就是head节点到环口的位置的距离是环的整数倍
  • 代码:
class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head, fast = head;//快慢指针
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (fast == slow) {//相遇
                while (slow != head) {//满足head节点到环口的位置的距离是环的整数倍时,找到环口
                    slow = slow.next;
                    head = head.next;
                    //fast = fast.next.next;
                }
                return slow;
            }
        }
        return null;
    }
}
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值