代码随想录算法训练营第三天| 203 移除链表元素 707 设计链表 206 反转链表

目录

203 移除链表元素

带虚拟头节点

不带虚拟头节点

不带pre节点

206 反转链表

递归

迭代

707 设计链表

单链表


203 移除链表元素

如果想删除链表中的一个元素,需要先找到它的上一个节点,然后再进行删除操作。

可以通过设置虚拟头节点的方式将头节点与其他节点统一起来进行操作,也可以先判断头节点的值确保头节点的值不为val。

带虚拟头节点

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode ln = new ListNode(-1,head);
        ListNode pre = ln;
        ListNode cur = head;
        while(cur != null){
            if(cur.val == val)pre.next = cur.next;
            else pre = cur;
            cur = cur.next;
        }
        return ln.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 pre = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == val)pre.next = cur.next;
            else pre = cur;
            cur = cur.next;
        }
        return head;
    }

不带pre节点

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while(head != null && head.val == val)head = head.next;
        ListNode cur = head;
        while(cur != null){
            if(cur.next != null && cur.next.val == val){
                cur.next = cur.next.next;
            }
            else cur = cur.next;
        }
        return head;
    }
}


206 反转链表

递归

class Solution {
    public ListNode reverseList(ListNode head) {
        return reverse(null,head);
    }
    public ListNode reverse(ListNode pre,ListNode cur){
        if(cur == null)return pre;
        ListNode tmp = cur.next;
        cur.next = pre;
        
        return reverse(cur,tmp);
    }
}

迭代

​
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        ListNode temp = null;
        while(cur != null){
            temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
}

​

707 设计链表

单链表

class ListNode{
    int val;
    ListNode next;
    ListNode(){}
    ListNode(int val){this.val = val;}
}

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 cur = 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(size,val);
    }
    public void addAtIndex(int index, int val) {
        if(index > size)return;
        size++;
        ListNode pre = head;
        for(int i = 0;i < index;i++)pre = pre.next;
        ListNode newNode = new ListNode(val);
        newNode.next = pre.next;
        pre.next = newNode;
    }
    
    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size)return;
        size--;
        if(index == 0){
            head = head.next;
            return;
        }
        ListNode pre = head;
        for(int i = 0;i < index;i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }
}

双链表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值