单链表的创建及增删查验

6 篇文章 0 订阅
1 篇文章 0 订阅

1.单链表的初始化

单链表是由节点构成的,所以,在创建单链表时,我们首先应该创建构成单链表的节点

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

这里我们设定了节点类,其中val用于存储节点值,next是指向下一个ListNode的引用(c里面称为指针),构造函数初始化对象的值。

然后,我们构造单链表,在单链表中,我们设定一个哨兵节点指向头节点方便我们进行操作,所以在链表类中,我们要初始化哨兵节点。

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }
}

这样,我们的初始化就完成了。

后面,我们需要将对应的方法写入链表类中。

2.增加节点

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }

    public void add(int location, int val){
        ListNode pre = dummy; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }
}

3.删除节点

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }

    public void add(int location, int val){
        ListNode pre = dummy; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }

    public void remove(int location){
        ListNode pre = dummy; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }
}

这里删除节点我们不考虑location超过链表长度或链表空无法继续删除的情况,以简化操作。

4.查询节点

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }

    public void add(int location, int val){
        ListNode pre = dummy; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }

    public void remove(int location){
        ListNode pre = dummy; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }

    public int get(int location){
        ListNode cur = dummy.next; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            cur = cur.next;
        }
        return cur.val;
    }
}

5.验证是否包含某节点

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }

    public void add(int location, int val){
        ListNode pre = dummy; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }

    public void remove(int location){
        ListNode pre = dummy; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }

    public int get(int location){
        ListNode cur = dummy.next; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            cur = cur.next;
        }
        return cur.val;
    }

    public boolean contain(int val){
        ListNode head = dummy.next; //初始时指向哨兵节点
        while(head != null){
            if(head.val = val){
                return True;
            }
            head = head.next;
        }
        return False;
        
    }
}

6.设置打印链表的函数

public class LinkedList{
    public LinkedList(){
        ListNode dummy = new ListNode(-1);
    }

    public void add(int location, int val){
        ListNode pre = dummy; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        ListNode node = new ListNode(val);
        node.next = pre.next;
        pre.next = node;
    }

    public void remove(int location){
        ListNode pre = dummy; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }

    public int get(int location){
        ListNode cur = dummy.next; //初始时指向哨兵节点
        for(int i = 0; i < location; i++){
            cur = cur.next;
        }
        return cur.val;
    }

    public boolean contain(int val){
        ListNode head = dummy.next; //初始时指向哨兵节点
        while(head != null){
            if(head.val = val){
                return True;
            }
            head = head.next;
        }
        return False;
        
    }
    public void print(){
        ListNode head = dummy.next;
        while(head != null){
            System.out.print(head.val + "->");
            head = head.next;
        }
        System.out.println("null");
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值