题解 | #在链表中删除指定值的节点#

import java.io.;
import java.util.
;

class Node{
public int value;
public Node next;
public Node(int data){
this.value=data;
}

//String数组转链表   非环
public  static Node tranStringNoLoop(String[] nums){
    Node head=new Node(Integer.parseInt(nums[0]));
    Node cur=head;
    for(int i=1;i<nums.length;i++){
        cur.next=new Node(Integer.parseInt(nums[i]));
        cur=cur.next;
    }
    return head;
}

//int数组转链表  非环
public  static Node tranIntegerNoLoop(int[] nums){
    Node head=new Node(nums[0]);
    Node cur=head;
    for(int i=1;i<nums.length;i++){
        cur.next=new Node(nums[i]);
        cur=cur.next;
    }
    return head;
}

//打印链表[非环]
public static  void printNodeList(Node head){
    StringBuilder sb=new StringBuilder();
    while (head!=null){
        sb.append(head.value).append(" ");
        head=head.next;
    }
    System.out.println(sb.toString());
}

}

public class Main{

public static Node removeValue1(Node head,int num){
    Stack<Node>stack=new Stack<Node>();
    while(head!=null){
        if(head.value!=num){
            stack.push(head);
        }
        head=head.next;
    }

    while(!stack.isEmpty()){
        //反转
        stack.peek().next=head;
        //移动
        head=stack.pop();
    }

    return head;
}

public static void main(String[]args)throws IOException{
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    in.readLine();
    String[] nums=in.readLine().split(" ");
    int num=Integer.parseInt(in.readLine());
    Node head=Node.tranStringNoLoop(nums);
    head=removeValue1(head,num);
    Node.printNodeList(head);
}

}

全部评论

相关推荐

12-27 22:21
门头沟学院 Java
点赞 评论 收藏
分享
11-12 14:30
已编辑
广东科技学院 前端工程师
迷茫的小刺猬在迎接o...:前端岗位越来越少了,中小厂也更倾向全栈了,更不需要初级或者实习。可能就大厂才会有一些岗位,但是很看学历。
实习,投递多份简历没人回...
点赞 评论 收藏
分享
程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务