链表题目:707. 设计链表 、 203. 移除链表元素、 237. 删除链表中的节点 、19. 删除链表的倒数第 N 个结点 、83. 删除排序链表中的重复元素 、 82. 删除排序链表中的重复元素

707. 设计链表

思路:

这个我感觉只要注意index 和size的关系就可以,别越界了。

代码:

class MyLinkedList {

    ListNode dummyhead;
    int size;
    public MyLinkedList() {
        dummyhead = new ListNode(0);
        size = 0;
    }
    
    public int get(int index) {
        if(index >= size || index < 0)
            return -1;
        ListNode pre = dummyhead;
        for(int i = 0; i <= index; i++){
            pre = pre.next;
        }
        return pre.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;
        size ++;
        ListNode pre = dummyhead;
        while(index -- > 0){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }
    
    public void deleteAtIndex(int index) {
        if(index >= size || index < 0){
            return ;
        }
        ListNode pre = dummyhead;
        while(index -- > 0){
            pre = pre.next;
        }
        pre.next = pre.next.next;
        size --;
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */

203. 移除链表元素

思路:

考虑三个场景,第一个节点、中间的节点和最后一个节点。

代码:

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

 237. 删除链表中的节点

思路:

直接让当前节点的值变成下一个节点的值,指向下一个节点的下一个节点。

代码:

class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}

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

思路:

先定义一个cur指向head,定义一个虚拟头结点,顺便定义pre 指向他。

然后要求是倒数第n个,那我们先让cur走n个节点,然后按顺序往后遍历,当cur==null的时候,说明pre的下一个节点就是要删除的,就直接让pre.next = pre.next.next;

代码:

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode pre = dummyHead;
        ListNode cur = head;
        for(int i = 0; i < n; i++){
            cur = cur.next;
        }
        while(cur != null){
            cur = cur.next;
            pre = pre.next;
        }
        pre.next = pre.next.next;
        return dummyHead.next;
    }
}

83. 删除排序链表中的重复元素

思路:

定义一个节点cur指向head,然后开始遍历,当cur.val == cur.next.val,就让cur.next = cur.next.next。

代码:

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

        }
        return head;
    }
}

82. 删除排序链表中的重复元素

思路:

相比于上一题,我们要把重复的元素彻底删除,所以需要多定义一个nexts指针,执行cur.next

如果nexts.val == cur.val 就让nexts指向nexts.next,同时让cur也指向。

代码:

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode pre = dummyHead, cur = head;
        while(cur != null){
            ListNode nexts = cur.next;
            while(nexts != null && nexts.val == cur.val){
                nexts = nexts.next;
            }
            if(nexts != cur.next){
                pre.next = nexts;
                cur = nexts;
            }else{
                pre = cur;
                cur = nexts;
            }
        }
        return dummyHead.next;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值