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

文章介绍了如何在LeetCode中使用双指针和递归方法移除链表元素,以及如何反转和设计链表的基本操作,包括添加、删除和获取节点值。
摘要由CSDN通过智能技术生成

 203.移除链表元素

链接:. - 力扣(LeetCode)

细节:

1. 删元素想到用dummy,dummy.next  = head,不然就断了

2. 我认为做出来就行,没必要纠结不用dummy

class Solution {
    public ListNode removeElements(ListNode head, int val) {
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            if (head == null) return null;
            ListNode first = dummy;
             first.next = head;
            while (first.next != null){
                if (first.next.val == val){
                    first.next = first.next.next;
                }
                else{
                    first  = first.next;
                }
                
            }
            return dummy.next;
    }
}

206.反转链表

链接:. - 力扣(LeetCode)

细节:基础链表考察、面试容易考

 

 双指针:

1.需要把最后的null给弄出来,不然这样cur都走不到最后,反不过来

2.存下一个节点自然就想到用一个temp来存

3.节点交换顺序 cur.next = temp,最主要的是要让cur的下一个指向pre,这样完成一次反转

双指针
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;
    }
}

递归:

1.根据双指针的写法 / 思路来想

2.注意定义的方法类是什么规则?看视频回顾帮你拿下反转链表 | LeetCode:206.反转链表 | 双指针法 | 递归法_哔哩哔哩_bilibili

递归
跟着双指针的思路来走
class Solution {
    public ListNode reverseList(ListNode head) {
        return reverse(null, head);

    }
    public ListNode reverse(ListNode pre, ListNode cur){
        if (cur == null) return pre;
        ListNode temp = null;
        temp = cur.next;
        cur.next =  pre;
        return reverse(cur, temp);
    }
}

707.设计链表

链接:. - 力扣(LeetCode)

细节:

1.在进行插入第n时,在newnode.next = cur.next; cur.next = newnode这个顺序

2.获取第n个时,设置cur操作head(会变)

3.删除时找到第n个节点,找到是cur = cur.next,然后才是跨过去cur.next = cur.next.next

 

//单链表
class ListNode {
    int val;
    ListNode next;
    ListNode(){}
    ListNode(int val) {
        this.val=val;
    }
}
class MyLinkedList {
    //size存储链表元素的个数
    int size;
    //虚拟头结点
    ListNode head;

    //初始化链表
    public MyLinkedList() {
        size = 0;
        head = new ListNode(0);
    }

    //获取第index个节点的数值,注意index是从0开始的,第0个节点就是头结点
    public int get(int index) {
        //如果index非法,返回-1
        if (index < 0 || index >= size) {
            return -1;
        }
        ListNode cur = head;
        //包含一个虚拟头节点,所以查找第 index+1 个节点
        for (int i = 0; i <= index; i++) {
            cur = cur.next;
        }
        return cur.val;
    }

    //在链表最前面插入一个节点,等价于在第0个元素前添加
    public void addAtHead(int val) {
        addAtIndex(0, val);
    }

    //在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加
    public void addAtTail(int val) {
        addAtIndex(size, val);
    }

    // 在第 index 个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
    // 如果 index 等于链表的长度,则说明是新插入的节点为链表的尾结点
    // 如果 index 大于链表的长度,则返回空
    public void addAtIndex(int index, int val) {
        if (index > size) {
            return;
        }
        if (index < 0) {
            index = 0;
        }
        size++;
        //找到要插入节点的前驱
        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;
    }

    //删除第index个节点
    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;
    }
}

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值