20230913刷题记录_链表

20230913刷题记录
参考【代码随想录】来刷的。
关键词:链表、快慢指针、头结点
涉及到删除、插入第一个位置的元素时,使用头结点可以减少判断。

1 移除链表元素

力扣题目链接
简单,可使用头结点减少判断。

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

2 设计链表

力扣题目链接
简单,考虑逻辑就行了。
我的代码在头插和在尾差没写到位,可以直接调用addAtIndex

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

class MyLinkedList {

    private ListNode head;
    private int size;

    public MyLinkedList() {
        head = new ListNode();
        size = -1;
    }

    public int get(int index) {
        ListNode node = getNode(index);
        if (node == null)
            return -1;
        return node.val;
    }
    private ListNode getNode(int index) {
        if (index > size)
            return null;
        ListNode p = head;
        for (int i = -1; i < index; i++) {
            p = p.next;
        }
        return p;
    }

    public void addAtHead(int val) {
        ListNode p = new ListNode(val);
        p.next = head.next;
        head.next = p;
        ++size;
    }

    public void addAtTail(int val) {
        ListNode tail = getNode(size);
        tail.next = new ListNode(val);
        ++size;
    }

    public void addAtIndex(int index, int val) {
        if (index > size + 1) 
            return;
        ListNode prev = getNode(index - 1);
        ListNode temp = new ListNode(val);
        temp.next = prev.next;
        prev.next = temp;
        ++size;
    }

    public void deleteAtIndex(int index) {
        if (index > size)
            return;
        ListNode prev = getNode(index - 1);
        prev.next = prev.next.next;
        --size;
    }
}

3 反转链表

力扣题目链接
简单

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode cur = head;
        ListNode temp = null;
        while (cur != null) {
            temp = cur.next;// 保存下一个节点
            cur.next = prev;
            prev = cur;
            cur = temp;
        }
        return prev;
    }
}

4 两两交换链表中的节点

力扣题目链接
注意逻辑清晰,交换时修改next的顺序,引入头结点来简化交换链表中第一个和第二个结点的操作。

class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode customHead = new ListNode();
        customHead.next = head;
        ListNode cur = customHead;
        ListNode first; // 要交换的第一个
        ListNode second;    // 要交换的第二个
        while (cur.next != null && cur.next.next != null) {
            first = cur.next;
            second = first.next;
            cur.next = second;
            first.next = second.next;
            second.next = first;
            cur = first;
        }
        return customHead.next;
    }
}

5 删除链表的倒数第N个结点

力扣题目链接
在这里插入图片描述

你能尝试使用一趟扫描实现吗?

快慢指针

设置一个快指针fast,慢指针slow。
fast比slow先走N步,然后让slow跟fast一起走,当fast指向最后一个元素的时候,slow指向要删除节点的上一个节点。

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        // 引入头节点,不然当删除第一个元素的时候要判断
        ListNode customHead = new ListNode();
        customHead.next = head;
        ListNode fast = customHead;
        ListNode slow = customHead;
        // fast先走n步
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        // 此时slow指向要删除的元素的上一个元素
        slow.next = slow.next.next;
        return customHead.next;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值