单链表

33 篇文章 0 订阅
32 篇文章 0 订阅

单链表常见习题总结1:
1:头插法

//1:头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);//要插入的节点,进行初始化
        // 1.是否是第一次插入:因为head直接给插进去的第一个节点 不用再写node.next=head
        if (head == null) {
            this.head = node;
        } else {
            node.next = this.head;
            this.head = node;
        }
    }

2:尾插法

//2:尾插法:思想:要找到单链表的尾巴,当cur.next=null时,就是最后一个节点
    public void addLast(int data) {
        ListNode node = new ListNode(data);//初始化一个节点
        ListNode cur = this.head;
        if (head == null) {//单链表是空
            this.head = node;
        } else {
            while (cur.next != null) {//cur.next==null 说明是最后一个节点
                cur = cur.next;
            }
            cur.next = node;
        }
    }

3:打印单链表

//3:打印单链表
  //思想:从头开始打印,直到尾结点
  public void disPlay() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

4:查找是否包含关键字key

 //4:查找是否包含关键字key
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.data == key) {
                return true;
            } else {
                cur = cur.next;//进行下一个节点的查找
            }
        }
        return false;//单链表都遍历完了都没有找到即跳出循环,返回false
    }

5:求单链表的长度

//5:求单链表的长度
   //思想:定义一个变量count,遍历单链表,遍历一个节点count++即可
    public int getLength() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

6:找cur的前驱

  private ListNode searchIndex(int index) {
        ListNode cur = this.head;
        //cur→index-1
        int count = 0;
        while (count < index - 1) {
            count++;
            cur = cur.next;
        }
        return cur;
    }

7:任意位置插入,第一个数据节点为0 号下标

//7:任意位置插入,第一个数据节点为0 号下标
    public boolean addIndex(int index, int data) {
        //1:判断插入的位置是否是合法的
        if (index < 0 || index > getLength()) {
            System.out.println("插入位置不合法");
            return false;
        }
        //2:当单链表为空的时候
        if (index == 0) {
            addFirst(data);//单链表为空的时候进行头插和尾插都是一样的
            return true;
        }
        //3:任意位置进行插入:先要找到前驱,因为单链表是单向的,所以要找到插入的前一项即index-1
        ListNode cur = searchIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
        return true;
    }

8:查找key的前驱

//思想:当(prev.next.data == key)的时候,返回prev就是该节点的前驱
 private ListNode searchPrev(int key) {
        ListNode prev = this.head;
        //最后一个节点,且头节点已经判断过了
        while (prev.next != null) {
            if (prev.next.data == key) {
                return prev;
            } else {
                prev = prev.next;
            }
        }
        return null;
    }

9:删除第一次出现关键字为key的节点

//9:删除第一次出现关键字为key的节点
    public void remove(int key) {
        //0:单链表为空的时候
        if (this.head == null) {
            System.out.println("单链表为空");
            return;
        }
        //1:删除的是头结点 直接就是head=head.next;
        if (this.head.data == key) {
            this.head = this.head.next;
            return;//只删一个
        }
        //2:找到关键字key的前驱
        ListNode prev = searchPrev(key);
        if (prev == null) {
            return;
        } else {
            ListNode del = prev.next;
            prev.next = del.next;
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值