【算法刷题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 小时前
力扣73:矩阵置零
算法·leetcode·矩阵
岁忧1 小时前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油1 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
月盈缺4 小时前
学习嵌入式的第二十二天——数据结构——双向链表
数据结构·学习·链表
猿究院--王升4 小时前
jvm三色标记
java·jvm·算法
一车小面包4 小时前
逻辑回归 从0到1
算法·机器学习·逻辑回归
tt5555555555556 小时前
字符串与算法题详解:最长回文子串、IP 地址转换、字符串排序、蛇形矩阵与字符串加密
c++·算法·矩阵
元亓亓亓7 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
不会学习?7 小时前
算法03 归并分治
算法
NuyoahC8 小时前
笔试——Day43
c++·算法·笔试