【算法刷题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;
    }
}

总结

暂无

相关推荐
এ᭄画画的北北33 分钟前
力扣-35.搜索插入位置
数据结构·算法·leetcode
cylat43 分钟前
Day23 pipeline管道
人工智能·python·算法·机器学习
lucky_jiexia1 小时前
leetcode刷题经验
算法·leetcode·哈希算法
蓝澈11212 小时前
数据结构之常用排序算法(冒泡、选择等)
数据结构·算法·排序算法
有梦想的骇客7 小时前
书籍将正方形矩阵顺时针转动90°(8)0605
线性代数·算法·矩阵
有梦想的骇客7 小时前
书籍“之“字形打印矩阵(8)0609
java·算法·矩阵
Chenyu_3108 小时前
12.找到字符串中所有字母异位词
c语言·数据结构·算法·哈希算法
苏三福8 小时前
yolo11-seg ultralytics 部署版本
算法·yolo11
wuqingshun31415911 小时前
蓝桥杯 冶炼金属
算法·职场和发展·蓝桥杯