链表题目总结

一、环形链表插值练习在这里插入代码片二、访问单个节点的删除练习题在这里插入代码片
摘要由CSDN通过智能技术生成

一、链表插值练习

在这里插入图片描述
下面这个代码实现的是无环单链表的插入

/*
public class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}*/
public class InsertValue {
   
    public ListNode insert(int[] A, int[] nxt, int val) {
   
        // write code here
        //如果A为空的情况
        if(A == null){
   
            return new ListNode(val);
        }
        //构建链表
        ListNode head = new ListNode(A[0]);//头节点
        ListNode cur = head; 
        for(int i = 0;i<A.length-1;i++){
   
            ListNode node = new ListNode(A[nxt[i]]);
            cur.next = node;
            //node.next = cur.next;
            cur = cur.next;
        }
        //将值等于val的节点加入到链表中
        ListNode pre = head;
        ListNode next = head.next;
        ListNode newNode = new ListNode(val);
        if(head.val >= val){
   
            newNode.next = head;
            //cur.next = newNode;
            head = newNode;
            return head;
        }
        while(next!=null){
   
            if(next.val >= val){
   
                pre.next = newNode;
                newNode.next = next;
                return head;
            }else{
   
                pre = next;
                next = next.next;
            }
        }
        newNode.next = next;
        pre.next = newNode;
        return head;
    }
}

二、访问单个节点的删除练习题

在这里插入图片描述

import java.util.*;
/*
public class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Remove {
   
    public ListNode removeNode(ListNode pNode, int delVal) {
   
        // write code here
        if(pNode==null){
   
            return null;
        }
        if(pNode.val == delVal){
   
            return pNode.next;
        }
        ListNode pre = pNode;
        ListNode cur = pNode.next; //被删除的节点
        while(cur != null){
   
            if(cur.val == delVal){
   
                pre.next = cur.next;
                break;
            }
            cur = cur.next;
            pre = pre.next;
        }
        return pNode;
    }
}

三、链表的分化练习题

在这里插入图片描述

import java.util.*;
/*
public class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Divide {
   
    public ListNode listDivide(ListNode head, int val) {
   
        // write code here
        ListNode sh = null;
        ListNode st = null;
        ListNode bh = null;
        ListNode bt = null;
        ListNode next = null;
        while(head != null){
   
            next = head.next;
            head.next = null;
            if(head.val <= val){
   
                if(sh == null){
   
                    sh = head;
                    st = head;
                }else{
   
                    st.next = head;
                    st = head;
                }
            }else{
   
                if(bh == null){
   
                    bh = head;
                    bt = head;
                }else{
   
                    bt.next = head;
                    bt 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值