第三天 | 203.移除链表元素,707.设计链表

 # 203_题目考察点:链表

## 题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

### 代码实现

package LeetCode;

public class YangSibo_203 {
     public  static void main (String args []) {
          ListNode headinit = new ListNode();
          ListNode head = headinit;
          int arr [] = {1,2,6,3,4,5,6};
          for (int i = 0; i < arr.length; i++ ) {
               ListNode temp = new ListNode(arr[i],null);
               head.next = temp;
               head = head.next;
          }
          headinit = headinit.next;
          YangSibo_203_1 demo1 = new YangSibo_203_1();
          ListNode resdemo1 = demo1.removeElements(headinit,6);
          System.out.print("demo1 : [");
          while(resdemo1 != null) {
               System.out.print(resdemo1.val);
               System.out.print(" ");
               resdemo1 = resdemo1.next;
          }
          System.out.print("]");
          System.out.println();
          YangSibo_203_2 demo2 = new YangSibo_203_2();
          ListNode resdemo2 = demo2.removeElements(headinit,6);
          System.out.print("demo2 : [");
          while(resdemo2 != null) {
               System.out.print(resdemo2.val);
               System.out.print(" ");
               resdemo2 = resdemo2.next;
          }
          System.out.print("]");
     }
}



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

class YangSibo_203_1 {
     public ListNode removeElements(ListNode head, int val) {
          if (head == null ) {
               return head;
          }
          while (head != null && head.val == val) {
               head= head.next;
          }
          ListNode current = head;
          while (current != null && current.next != null ){
               if (current.next.val == val){
                    current.next = current.next.next;
               }else {
                    current = current.next;
               }
          }
          return head;
     }
}
class YangSibo_203_2 {
     public ListNode removeElements(ListNode head, int val) {
          if (head == null ) {
               return head;
          }
          ListNode virtualhead = new ListNode(0,head);
          ListNode res = virtualhead;
          while (virtualhead != null && virtualhead.next != null ){
               if (virtualhead.next.val == val){
                    virtualhead.next = virtualhead.next.next;
               }else {
                    virtualhead = virtualhead.next;
               }
          }
          return res.next;
     }
}

### 解题注意事项

此处可以在处理head节点的特殊处理下,也可以使用虚拟头结点保证整个流程的统一,使用虚拟头节点会更加耗内存

 # 707_题目考察点:链表

 ## 题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

### 代码实现-未进行调试

package LeetCode;

import java.awt.*;
import java.util.List;

public class YangSibo_707 {
    public static void main (String args []) {
        MyLinkedList demo = new MyLinkedList();
        demo.addAtHead(1);
        demo.addAtTail(3);
        demo.addAtIndex(1, 2);
        demo.get(1);
        demo.deleteAtIndex(1);
        demo.get(1);
    }
}


class MyLinkedList {
    MyListNode virtulhead;
    public MyLinkedList() {
        MyListNode head = new MyListNode();
        this.virtulhead = new MyListNode(-1, head);
    }

    public int get(int index) {
        MyListNode getdemo = this.virtulhead;
        int res = 0;
        int count = 0;
        while(getdemo != null) {
            res = getdemo.val;
            count ++;
            if(count == index && getdemo.next == null){
                return -1;
            }
        }
        return res;
    }

    public void addAtHead(int val) {
        MyListNode addheaddemo = this.virtulhead;
        MyListNode tempvirtulhead = new MyListNode(val,addheaddemo);
        this.virtulhead = tempvirtulhead;
    }

    public void addAtTail(int val) {
        MyListNode addtaildemo = this.virtulhead;
        while (addtaildemo.next != null) {
            addtaildemo = addtaildemo.next;
        }
        MyListNode tempdemo = new MyListNode(val);
        addtaildemo.next = tempdemo;
    }

    public void addAtIndex(int index, int val) {
        if(index == 0){
            addAtHead(val);
        }else {
            MyListNode addindexdemo = this.virtulhead;
            int count = 1;
            int flag = 0;
            while (addindexdemo.next != null) {
                count ++;
                if (count == index) {
                    MyListNode tempindexdemo = new MyListNode(val,addindexdemo.next);
                    addindexdemo.next = tempindexdemo;
                    flag = 1;
                }
            }
            if(flag == 0 && (count-1) == index) {
                addAtTail(val);
            }
        }
    }

    public void deleteAtIndex(int index) {
        if (index == 0) {
            this.virtulhead = this.virtulhead.next;
        }else {
            MyListNode deletedemo = this.virtulhead;
            int count = 1;
            while (deletedemo.next != null) {
                if (count == index) {
                    deletedemo.next = deletedemo.next.next;
                }

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

# 206_题目考察点:反转链表

## 题目链接: 力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

代码实现-此题目未完成

可参考此链接:代码随想录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值