【算法刷题day3】Leetcode: 203.移除链表元素、707.设计链表、 206.反转链表

文章目录

草稿图网站
java的Deque

Leetcode 203.移除链表元素

题目: 203.移除链表元素
解析: 2.5 练习时长两年半

解题思路

使用虚拟头节点,判定下一个节点是否为val,若为val,则删除下一个节点

代码

java 复制代码
/**
 * 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) {
        ListNode dummy = new ListNode(-1, head);
        ListNode cur = dummy;
        while (cur.next != null){
            if (cur.next.val == val){
                cur.next = cur.next.next;
            }else{
                cur = cur.next;
            }
        }
        return dummy.next;

    }
}

总结

没试过不适用虚拟头节点

java 复制代码
/**
 * 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) {
        while (head != null && head.val == val){
            head = head.next;
        }
        if (head == null)
            return head;
        ListNode cur = head;
        while (cur.next != null){
            if (cur.next.val == val)
                cur.next = cur.next.next;
            else
                cur = cur.next;
        }
        return head;

    }
}

Leetcode 707.设计链表

题目: 707.设计链表
解析: 2.5 练习时长两年半

解题思路

正常思路

代码

java 复制代码
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 MyLinkedList {

    int size;
    ListNode head;

    public MyLinkedList() {
        size = 0;
        head = new ListNode(0);
    }
    
    public int get(int index) {
        if (index < 0 || index >= size)
            return -1;
        ListNode curNode = head.next;
        for (int i = 0; i < index; i++)
            curNode = curNode.next;
        return curNode.val;
    }
    
    public void addAtHead(int val) {
        addAtIndex(0, val);
    }
    
    public void addAtTail(int val) {
        addAtIndex(size, val);
    }
    
    public void addAtIndex(int index, int val) {
        if (index > size)
            return;
        if (index < 0)
            index = 0;
        ListNode pre = head;
        for(int i = 0; i < index; i++)
            pre = pre.next;
        ListNode toAdd = new ListNode(val);
        toAdd.next = pre.next;
        pre.next = toAdd;
        size++;
    }
    
    public void deleteAtIndex(int index) {
        if (index < 0 || index >= size)
            return;
        ListNode pre = head;
        for(int i = 0; i < index; i++)
            pre = pre.next;
        pre.next = pre.next.next;
        size--;
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */

总结

出现了奇怪的bug,全部删掉重新写一遍,没有bug了。

Leetcode 206. 反转链表

题目: 206. 反转链表
解析: 2.5 练习时长两年半

解题思路

头插法翻转链表

代码

java 复制代码
/**
 * 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 reverseList(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode cur = head;
        while (cur != null && cur.next != null){
            ListNode tmp = cur.next;
            cur.next = tmp.next;
            tmp.next = dummy.next;
            dummy.next = tmp;
        }
        return dummy.next;
    }
}

总结

暂无

相关推荐
羑悻的小杀马特1 小时前
从工厂到生活:算法 × 深度学习,正在改写自动化的底层逻辑
深度学习·算法·自动化·生活
翀哥~2 小时前
奇偶ASCII值判断
c++·算法·ascii
凯思软件3 小时前
Abaqus应用场景解析:从汽车碰撞到航空航天非线性分析
人工智能·算法·机器学习
不吃香菜?9 小时前
逻辑回归在信用卡欺诈检测中的实战应用
算法·机器学习·逻辑回归
Kay_Liang9 小时前
探究排序算法的奥秘(下):快速排序、归并排序、堆排序
java·数据结构·c++·python·算法·排序算法
禺垣9 小时前
AdaBoost算法的原理及Python实现
人工智能·python·算法·机器学习·数据挖掘·adaboost·集成学习
qq_508576099 小时前
混淆矩阵(Confusion Matrix)横坐标
人工智能·算法·机器学习
LSQ的测试日记10 小时前
机器学习_KNN算法
人工智能·算法·机器学习
炬火初现10 小时前
[leetcode]2302.统计得分小于k的子数组
算法·leetcode·职场和发展
鱼嘻10 小时前
数据结构------C语言经典题目(6)
linux·c语言·开发语言·数据结构·算法