栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

机试练习01——删除链表的倒数第n个结点

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

机试练习01——删除链表的倒数第n个结点

目录

输入输出练习

删除倒数第n个结点并返回

构造单链表

头插法&尾插法生成单链表

返回倒数第n个链表

返回删除倒数第n个结点后的链表

从单向链表中删除指定值的节点


输入输出练习

调用方法

import java.util.Scanner;
Scanner scanner=new Scanner(System.in);

机试上对于输入没有确定,结合scanner.hasNext() 和 scanner.nextInt() scanner.nextLine()进行输入。

要注意的是 scanner.hasNext()在本地idea上不会结束,而是一直等待输入。如果容器中有输入为true,没有则会一直等待输入,进入阻塞。

但在在线编程界面时,输入的是文本可以直接退出。

import java.util.Scanner;
public class testScanner {
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.println("开始测试**************");
        String x;
        while(scanner.hasNext()){
            x=scanner.nextLine();
            //scanner.nextInt();//输入整数
            System.out.println(x);
        }
        System.out.println("测试结束*****************");
    }

}

删除倒数第n个结点并返回

构造单链表
class Node{
    public int val;
    public Node next;
    public Node(){
        this.next=null;
    }
    public Node(int val){
        this.val=val;
        this.next=null;
    }
}

头插法&尾插法生成单链表
//头插法生成链表
Node head=new Node(-1);//头结点
Node tem=head;
for(int i=0; i 
//尾插法   尾插法和输入顺序是倒序    所以不适合本题的输入输出
Node head=null;
for(int i=0 ;i 

返回倒数第n个链表
import java.util.Scanner;
public class removeNthNode2 {
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
//            int num=scanner.nextInt();
            //System.out.println(11111111);
            int num=5;
            //生成 一个链表
            Node head=new Node(-1);//头结点
            //头插法生成链表
            Node tem=head;
            for(int i=0; i 

返回删除倒数第n个结点后的链表

除去倒数第n个链表的代码

private static int count=0;
private static ListNode fun(ListNode head , int n){
   if(head==null) return head;
   head.next=fun(head.next,n);
   count++;
   if(count==n){
        return head.next;
   }
   return head;
}

从单向链表中删除指定值的节点

全部手写

//删除 容易  主要构造链表难
//输入两个数字  a b   在链表 list中找到  b 然后插入a
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            //输入数据都是一行
            //输出数据链表值+空格
            int num=sc.nextInt();
            int h=sc.nextInt();
            Node head=new Node(h);
            
            //头结点已经定义  后续结点 少一个
            for(int i=0; ileft
                fun(head,left,right);
            }
            int del=sc.nextInt();
            deleNode(head,del);
            //输出结点
            for(int i=0; ileft
        if(head==null) return;
        Node  cur=head;
        while(cur!=null && cur.val!=r){
            cur=cur.next;
        }
        if(cur!=null && cur.val==r){
            Node tem=cur.next;
            cur.next=new Node(l);
            cur.next.next=tem;
        }
    }
    
    private static void deleNode(Node head,int del){
        if(head==null) return;
        Node pre=new Node();
        pre.next=head;
        Node cur=head;
        while(cur!=null && cur.val!=del){
            pre=pre.next;
            cur=cur.next;
        }
        if(cur!=null && cur.val==del){
            pre.next=cur.next;
            cur.next=null;
        }
        
     }
}

class Node{
    public int val;
    public Node next;
    public Node(){
        this.next=null;
    }
    public Node(int val){
        this.val=val;
        this.next=null;
    }
}

稍微繁琐,可以利用 List集合来写。

list.indexOf(node)可以返回其所在位置

list.add(index,newNode);

import java.util.Scanner;
import java.util.List;
import java.util.LinkedList;
public class Main{
    public static void main(String[]args){
        Scanner scan =new Scanner(System.in);
        while(scan.hasNext()){
            int num =scan.nextInt();
            Listlist = new LinkedList();
            int first =scan.nextInt();
            list.add(first);
            for(int i=0;i 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978880.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号