算法训练营day3

Leetcode 203 移除链表元素

题目链接:203. 移除链表元素 - 力扣(LeetCode)

/**
 * 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;
        }
        //考虑非头节点
        ListNode cur = head;
        while(cur != null && cur.next != null) {
            if(cur.next.val == val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return head; 
    }
}
/**
 * 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);
        dummy.next = head;
        ListNode cur = dummy;
        while(cur != null && cur.next != null) {
            if(cur.next.val == val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return dummy.next;
    }
}

Leetcode 707 设计链表

题目链接:707. 设计链表 - 力扣(LeetCode)

//创建一个 Node 类 
class Node {
    public int val;
    public Node next;
    public Node(int val) {
        this.val = val;
    }
}
class MyLinkedList {
    public int size;
    public Node head;
    public MyLinkedList() {
        this.size = 0;
        //创建一个虚拟头节点
        this.head = new Node(-1);
    }
    
    public int get(int index) {
        if(index < 0 || index >= this.size) {
            return -1;
        }
        Node cur = this.head;
        for(int i = 0; i <= index; i++) {
            cur = cur.next;
        }
        return cur.val;
    }
    
    public void addAtHead(int val) {
        addAtIndex(0, val);
    }
    
    public void addAtTail(int val) {
        addAtIndex(this.size, val);
    }
    
    public void addAtIndex(int index, int val) {
        if(index > this.size) {
            return;
        }
        if(index < 0) {
            index = 0;
        }
        this.size++;
        Node cur = this.head;
        // 找到 index - 1 节点
        for(int i = 0; i < index; i++) {
            cur = cur.next;
        }
        //构建将要插入的节点
        Node node = new Node(val);
        node.next = cur.next;
        cur.next = node;
    }
    
    public void deleteAtIndex(int index) {
        if(index < 0 || index >= this.size) {
            return;
        }
        this.size--;
        if(index == 0) {
            this.head = this.head.next;
            return;
        }
        Node cur = this.head;
        for(int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.next = cur.next.next;
    }
}

/**
 * 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);
 */

Leetcode 206 反转链表

题目链接:206. 反转链表 - 力扣(LeetCode)

/**
 * 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) {
        //判断头节点是否为空
        if(head == null) {
            return head;
        }
        ListNode pre = head;
        ListNode cur = null;
        while(pre != null) {
            ListNode next = pre.next;
            pre.next = cur;
            cur = pre;
            pre = next;
        }
        return cur;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值