代码随想录算法训练营第三天| LeetCode203 移除链表元素、LeetCode707 设计链表、LeetCode206 反转链表

LeetCode203 移除链表元素

题目/文章链接:https://programmercarl.com/%E9%93%BE%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html方法:两种方法

public class code203 {

    // 只使用一个当前节点不使用虚拟头节点
    public ListNode removeElements(ListNode head, int val) {

        while (head != null && head.val == val) {
            head = head.next;

        }

        ListNode currentNode = head;

        while (currentNode != null) {
            while (currentNode.next != null && currentNode.next.val == val) {
                currentNode.next = currentNode.next.next;
            }
            currentNode = currentNode.next;
        }
        return head;
    }
    // 使用虚拟头节点
    public ListNode removeElements2(ListNode head, int val) {

        if (head == null) {
            return head;
        }

        ListNode dummy = new ListNode(-1, head);
        ListNode pre = dummy;
        ListNode curr = head;

        while (curr != null) {
            if (curr.val == val) {
                pre.next = curr.next;
            } else {
                pre = curr;
            }
            curr = curr.next;
        }
        return dummy.next;
    }
}

心得:使用虚拟头节点可以使两端的节点一般化

LeetCode707 设计链表

题目/文章链接:https://programmercarl.com/0707.%E8%AE%BE%E8%AE%A1%E9%93%BE%E8%A1%A8.html

代码如下:

public class MyLinkedList {

    int size; // 链表的节点数
    ListNode dummyhead; // 虚拟头节点
    // 初始化

    public MyLinkedList() {
        size = 0;
        dummyhead = new ListNode(0);
    }

    // 获取指定索引处链表的值,索引下表从0开始
    public int get(int index) {
        if (index < 0 || index > size - 1) {
            return -1;
        }
        ListNode curNode = dummyhead;
        for (int i = 0; i < index + 1; 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 = dummyhead;
        // 找到要插入位置索引的前一个节点
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        ListNode add = new ListNode(val);
        // 插入操作
        add.next = pre.next;
        pre.next = add;
        size++; // 注意别忘了长度加一

    }

    // 删除指定索引处的元素
    public void deleteAtIndex(int index) {

        if (index < 0 || index > size - 1) {
            return;
        }


        ListNode pre = dummyhead;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        pre.next = pre.next.next;

        size--;
    }
}

心得:熟悉了链表的常用基本操作

LeetCode206 反转链表

题目链接:https://programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html双指针法代码如下:

public class code206 {

    // 双指针法
    public ListNode reverseList(ListNode head) {

        ListNode cur = head;
        ListNode pre = null;
        ListNode temp = null;

        while (cur != null) {
            temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;

        }
        return pre;
    }

}

递归法:

public class code206Recursion {

    public ListNode reverseList(ListNode head) {
        return reverse(null, head);

    }

    private ListNode reverse(ListNode pre, ListNode cur) {

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

        return reverse(cur, temp);


    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值