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

这篇博客介绍了多种链表操作,包括使用头插法和尾插法构造单链表,删除链表中倒数第n个节点,以及从链表中删除指定值的节点。还涉及到了输入输出的处理,如使用Scanner类读取用户输入,并提供了质数因子的计算方法。

目录

输入输出练习

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

构造单链表

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

返回倒数第n个链表

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

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

HJ6 质数因子


目录

输入输出练习

删除倒数第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<num ; i++){
    int v=scanner.nextInt();
    tem.next=new Node(v);
    tem=tem.next;
}
//尾插法   尾插法和输入顺序是倒序    所以不适合本题的输入输出
Node head=null;
for(int i=0 ;i<num  ; i++){
    int val=scanner.nextInt();
    Node tem=new Node(val);
    tem.next=head;
    head=tem;
}

返回倒数第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<num ; i++){
                int v=scanner.nextInt();
                tem.next=new Node(v);
                tem=tem.next;
            }
            //返回 倒数第n个结点
            int n=scanner.nextInt();
            //head=head.next;
            for(int i=1; i<=num-n+1; i++){
                head=head.next;
            }
            System.out.println(head.val);
        }
    }
}
//构造链表
class Node{
    public int val;
    public Node next;
    public Node(){
        this.next=null;
    }
    public Node(int val){
        this.val=val;
        this.next=null;
    }
}

返回删除倒数第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; i<num-1; i++){
                int left=sc.nextInt();
                int right=sc.nextInt();
                //在head中找到right结点  并插入left  即 right-->left
                fun(head,left,right);
            }
            int del=sc.nextInt();
            deleNode(head,del);
            //输出结点
            for(int i=0; i<num-1;i++){
                System.out.print(head.val+" ");
                head=head.next;
            }
        }
    }
    
    private static void fun(Node head,int l ,int r){
        //在head中找到right结点  并插入left  即 right-->left
        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();
            List<Integer>list = new LinkedList<Integer>();
            int first =scan.nextInt();
            list.add(first);
            for(int i=0;i<num-1;i++){
                int newNode=scan.nextInt();
                int node =scan.nextInt();
                int index =list.indexOf(node);
                list.add(index+1,newNode);
            }
            int remvoe = scan.nextInt();
            list.remove(list.indexOf(remvoe));
            for(int value:list){
                System.out.print(value+" ");
            }
        }
    }
}

HJ6 质数因子

//保证一定存在吗   不存在的话 本身就是质数
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            long n=sc.nextLong();
//long n=Long.parseLong(sc.next());
            for(int i=2 ; i<= Math.sqrt(n) ;i++){
                while(n%i==0){
                    System.out.print(i);
                    n/=i;
                    System.out.print(" ");
                }
            }
            if(n!=1){
                System.out.print(n);
            }
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值