day3链表--链表理论基础 ● 203.移除链表元素 ● 707.设计链表 ● 206.反转链表

203.移除链表元素  

题目链接:203. 移除链表元素 - 力扣(LeetCode)

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while(head != null && head.val == val)
            head = head.next;

        ListNode p = head;

        while(p != null && p.next != null){
            if(p.next.val == val)
                p.next = p.next.next;
            p = p.next;
        }
    return head;
    }
}

总结:看似简单,却考了我一个java的基础知识, while(p != null && p.next != null)一定要加上p != null不然会报错,原因是p本身为空的情况下,不存在p.next,上面的head也是同理,这个知识点把我整了20来分钟,一开始看了半天也没看出来是为什么报错。

 707.设计链表 

题目链接:707. 设计链表 - 力扣(LeetCode)

class MyLinkedList {
    int size;
    ListNode head;

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

    public MyLinkedList() {
        size = 0;
        head = new ListNode(0);
    }
    
    public int get(int index) {
        if(index < 0 || index >= size)
            return -1;
        ListNode cur = head;
        for(int i = 0; i <= index; i++){
            cur = cur.next;
        }
        return cur.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;
        }

        index = Math.max(0, index);

        int i;
        ListNode cur = head;
        ListNode add = new ListNode(val);

        if(index == size){
            for( i = 0; i < index; i++){
                cur = cur.next;
            }
            cur.next = add;
            size++;
            return;
        }

        for( i = 0; i < index; i++){
            cur = cur.next;
        }
        add.next = cur.next;
        cur.next = add;
        size++;
        return;
    }
    
    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size)
            return;
            
        ListNode cur = head;

        if(index == 0){
            head = head.next;
            size--;
            return;
        }
            
        for(int i=1; i < index; i++){
            cur = cur.next;
        }
        cur.next = cur.next.next;
        size++;
        return;  
        
    }
}

总结:第一眼看感觉右手就行,结果写了好久,题目有点小问题,该链表默认是有头节点的,所以查找的时候注意index的位置,

然后就是增加和删除是需要改变size的,这一点一开始我没有注意

临界情况就是index分为三种,0,正常值,size,所以需要分析一下所处的条件。

 206.反转链表 

题目链接:206. 反转链表 - 力扣(LeetCode)

双指针法,(常规)

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode cur,tmp,pre;
        if(head == null || head.next == null)
            return head;
        pre = null;
        cur = head;
        while(cur != null){
            tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }
        head = pre;
        return head;
    }
}

递归法:

class Solution {
    public ListNode reverseList(ListNode head) {
        return reverse(head,null);
    }
    
    public ListNode reverse(ListNode cur,ListNode pre){
        if(cur == null) return pre;
        ListNode tmp = cur.next;
        cur.next = pre;
        return reverse(tmp,cur);
    }
}

总结,递归法不是很熟练,双指针法倒是容易想出来,

易错点是在于双指针法时,不能pre=head,cur=head.next,这样赋值,这会导致死循环。

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林中晚霞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值