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

链表基础: 代码随想录

 203.移除链表元素

题目链接: 力扣题目链接
文章讲解: 力扣题目链接

代码:

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

707.设计链表

    题目链接: 力扣题目链接
文章讲解: 力扣题目链接

代码:

class MyLinkedListNode {
    public int val;
    public MyLinkedListNode next;

    public MyLinkedListNode(int val, MyLinkedListNode next) {
        this.val = val;
        this.next = next;
    }
}

class MyLinkedList {

    private MyLinkedListNode head;
    private MyLinkedListNode tail;
    private int sum;

    public MyLinkedList() {
        this.head = null;
        this.tail = null;
        this.sum = 0;
    }

    public int get(int index) {
        if (index >= sum) return -1;
        MyLinkedListNode it = this.head;
        for (; index > 0; index--) {
            it = it.next;
        }
        return it.val;
    }

    public void addAtHead(int val) {
        MyLinkedListNode newNode = new MyLinkedListNode(val, this.head);
        this.head = newNode;
        if (this.tail == null) {
            this.tail = newNode;
        }
        this.sum++;
    }

    public void addAtTail(int val) {
        MyLinkedListNode newNode = new MyLinkedListNode(val, null);
        if (this.tail != null) {
            this.tail.next = newNode;
        }
        this.tail = newNode;
        if (this.head == null) {
            this.head = newNode;
        }
        this.sum++;
    }

    public void addAtIndex(int index, int val) {
        if (index > this.sum) return;
        if (index == this.sum) {
            addAtTail(val);
            return;
        }
        MyLinkedListNode it = this.head;
        for (int i = 0; i < index - 1; i++) {
            it = it.next;
        }
        MyLinkedListNode newNode = new MyLinkedListNode(val, it.next);
        it.next = newNode;
        this.sum++;
    }

    public void deleteAtIndex(int index) {
        if (index < 0 || index >= this.sum) return;
        if (index == 0) {
            this.head = this.head.next;
            if (this.head == null) {
                this.tail = null; // Empty list
            }
            this.sum--;
            return;
        }
        MyLinkedListNode it = this.head;
        for (int i = 0; i < index - 1; i++) {
            it = it.next;
        }
        it.next = it.next.next;
        if (it.next == null) {
            this.tail = it; // Update tail if the last node is deleted
        }
        this.sum--;
    }
}

206.反转链表

题目链接:力扣题目链接

文章讲解: 力扣题目链接

视频讲解:帮你拿下反转链表 | LeetCode:206.反转链表

 双指针法(常规,循环遍历)

代码:

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

递归法(妙但抽象)

代码:

class Solution {
    ListNode reverseList(ListNode head) {
        // 边缘条件判断
        if(head == null) return null;
        if (head.next == null) return head;
        
        // 递归调用,翻转第二个节点开始往后的链表
        ListNode last = reverseList(head.next);
        // 翻转头节点与第二个节点的指向
        head.next.next = head;
        // 此时的 head 节点为尾节点,next 需要指向 NULL
        head.next = null;
        return last;
    } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值