DAY3-链表相关-203,707,206

因为之前学过C语言,所以对链表的结构这块理解的还行。

203

失败代码:

public ListNode removeElements(ListNode head, int val) {
        ListNode l=new ListNode();
        l.next=head;
        while(l.next!=null){
            if(l.next.val==val){
                l=l.next.next;
            }
            l=l.next;
        }
        return l.next;
    }

写着写着就很离谱了。。。 

方法一:

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

其实在测试用例2就给了链表为空的情况,就应该想到要在开头去对链表是否为空做一个判断。

以下为不添加头节点的情况,多了一个判断头节点状态的步骤。

方法二:

public ListNode removeElements(ListNode head, int val) {
        while(head!=null&&head.val==val){
            head=head.next;
        }
        if(head==null){
            return head;
        }
        ListNode pre=head;
        ListNode cur=head.next;
        while(cur!=null){
            if(cur.val==val){
                pre.next=cur.next;
            }else{
                pre=cur;
            }
            cur=cur.next;
        }
        return head;
    }

 有一个问题,把if放在前面while放在后面,不停显示可能会出现空指针的情况,可是最开始不是已经判空了吗。最开始判空只能证明第一个不是空的,如果遇到测试用例3的情况,一直head=head.next,这个head已经为空了,next就更不知道操作到啥地方了。

方法三:

public ListNode removeElements(ListNode head, int val) {
        while(head!=null&&head.val==val){
            head=head.next;
        }
        ListNode cur=head;
        while(cur!=null){
            while(cur.next!=null&&cur.next.val==val){
                cur.next=cur.next.next;
            }
            cur=cur.next;
        }
        return head;
    }

自己最开始写的代码还是更接近第三种,但是一定注意链表里指针指向问题,一不小心就会操作空指针。

707

class MyLinkedList {
    int size;
    ListNode head;
    public MyLinkedList() {
        size=0;
        head = new ListNode(0);
    }
    
    public int get(int index) {
        //无效返回-1
        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;
        }
        if(index<0){
            index=0;
        }
        size++;
        ListNode pre=head;
        for(int i=0;i<index;i++){
            pre=pre.next;
        }
        ListNode addNode = new ListNode(val);
        addNode.next=pre.next;
        pre.next=addNode;
    }
    
    public void deleteAtIndex(int index) {
        if(index<0||index>=size){
            return;
        }
        size--;
        if(index==0){
            head=head.next;
            return;
        }
        ListNode pre = head;
        for(int i=0;i<index;i++){
            pre=pre.next;
        }
        pre.next=pre.next.next;
    }
}

 这道题考察了链表的增删改查,在涉及节点变动的时候,要找到前一个节点的位置,比如添加节点.

206

是能想到需要三个指针的,但还是不太会初始化,链表类的题一定要注意步骤

用到了三个指针,但是只是前后两个指针在移动,另外一个起到了保存当前指针下一个节点的功能,要不然一改变方向,下一个节点就失联了。

时间复杂度:O(n),空间复杂度:O(1)

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

递归的解法和双指针是很像的。

时间复杂度:O(n),空间复杂度:O(n)

递归调用了n层栈空间。

public ListNode reverseList(ListNode head) {
        if(head==null){
            return head;
        }  
        return reserse(null,head);
    }
    public ListNode reserse(ListNode pre,ListNode cur){
        if(cur==null){
            return pre;
        }
        ListNode temp=null;
        temp=cur.next;
        cur.next=pre;
        return reserse(cur,temp); 
    }
  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值