Sort by

recency

|

497 Discussions

|

  • + 0 comments

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        MyList list = new MyList();
        int n = sc.nextInt();
        for (int i=0 ; i<n ; i++){
            list.add(sc.nextInt());
        }
    
        int queryLength = sc.nextInt();
        sc.nextLine();
    
        for (int i=0 ; i<queryLength ; i++){
            String query = sc.next();
            if (query.equals("Insert")){
                int index = sc.nextInt();
                int value = sc.nextInt();
                list.insert(index, value);
            }
    
            if (query.equals("Delete")){
                int index = sc.nextInt();
                list.delete(index);
            }
        }
    
        list.print();
    }
    

    }

    class MyList{ Node node;

    public void add(int value){
    
        // check if list is empty
        if(node == null){
            node = new Node(value, null);
            return;
        }
    
        // find last node
        Node lastNode = node;
        while (lastNode.getNext() != null){
            lastNode = lastNode.getNext();
        }
        // create and add new node to last node
        Node newNode = new Node(value, null);
        lastNode.setNext(newNode);
    
    }
    
    
    public void insert(int index, int value){
    
        int length = length();
        if (index > length || index < 0){
            return;
        }
    
    
        // insert head
        if (index == 0){
            // Note: Handles if list is empty
            Node newNode = new Node(value, node);
            node = newNode;
    
            return;
        }
    
        // insert end
        if (index == length){
            // find last node
            Node lastNode = node;
            while (lastNode.getNext() != null){
                lastNode = lastNode.getNext();
            }
            lastNode.setNext(new Node(value, null));
    
            return;
        }
    
        // insert between
        int i=0;
        Node currentNode = node;
        while (currentNode != null && i<index-1){
            currentNode = currentNode.getNext();
            i++;
        }
        Node newNode = new Node(value,currentNode.getNext());
        currentNode.setNext(newNode);
    
    }
    
    public void delete(int index){
    
        int length = length();
        if (index > length-1){
            return;
        }
    
        // delete head
        if (index == 0){
    
            if (node == null){
                return;
            }
            Node secondNode = node.getNext();
            node.setNext(null);
            node = secondNode;
    
            return;
        }
    
        // delete end
        if (index == length-1){
    
            int i=0;
            Node currentNode = node;
            while (currentNode != null && i<index-1){
                currentNode = currentNode.getNext();
                i++;
            }
            currentNode.setNext(null);
    
            return;
        }
    
        // delete between
        int i=0;
        Node currentNode = node;
        while (currentNode != null && i<index-1){
            currentNode = currentNode.getNext();
            i++;
        }
        Node deletedNode = currentNode.getNext();
        currentNode.setNext(currentNode.getNext().getNext());
        deletedNode.setNext(null);
    
    }
    
    public int length(){
    
        if (node == null){
            return 0;
        }
    
        Node currentNode = node;
        int length = 0;
        while (currentNode != null){
            currentNode = currentNode.getNext();
            length++;
        }
    
        return length;
    }
    
    public void print() {
        Node current = node;
        while (current != null) {
            System.out.print(current.getValue());
            if (current.getNext() != null) {
                System.out.print(" ");
            }
            current = current.getNext();
        }
        System.out.println();
    }
    

    }

    class Node{ int val; Node next;

    public Node(int val, Node next){
        this.val = val;
        this.next = next;
    }
    
    public Node getNext(){
        return next;
    }
    
    public void setNext(Node next){
        this.next = next;
    }
    
    public int getValue(){
        return val;
    }
    
    public void setValue(int val){
        this.val = val;
    }
    

    }

  • + 0 comments
            int n = scan.nextInt();
            scan.nextLine();
            LinkedList<String> nList =  new LinkedList<>();
            nList.addAll(Arrays.asList(scan.nextLine().replaceAll("\\s+$", "").split(" ")));
      
            int q = scan.nextInt();
            scan.nextLine();
            for(int i=0;i<q;i++){
                String op = scan.nextLine();
                if(op.equals("Insert")){
                    nList.add(scan.nextInt(),scan.next());
                }else 
                if(op.equals("Delete")){   
                    nList.remove(scan.nextInt());
                }
                if(scan.hasNext())
                    scan.nextLine();
            }
            scan.close();
            nList.forEach(a-> System.out.print(a+" "));
    
  • + 0 comments

    public class Solution {

    public static void main(String[] args) {
    

    ** /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. /* Scanner sc = new Scanner(System.in); int n = sc.nextInt();

       List<Integer> list = new ArrayList<>();
    
       for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
       }
    
       int q = sc.nextInt();
       for (int i = 0; i < q; i++) {
            String command = sc.next();
    
            if (command.equalsIgnoreCase("Insert")) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                list.add(x, y);
    
            } else if (command.equalsIgnoreCase("Delete")) {
                int index = sc.nextInt();
                list.remove(index);
            }
       }
       for (Integer element: list) {
            System.out.print(element + " ");
       }
    }
    
  • + 0 comments
    public class Solution {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            sc.nextLine();
            List<Integer> li=Arrays.stream(sc.nextLine().trim().split(" ")).map(Integer::parseInt).collect(Collectors.toList());
            int n=sc.nextInt();
            while(n-->0){
                sc.nextLine();
                if(sc.nextLine().trim().equals("Insert")){
                    li.add(sc.nextInt(), sc.nextInt());
                }else{
                    li.remove(sc.nextInt());
                }
            }
            li.stream().forEach(ele->System.out.print(ele+" "));
        }
    }
    
  • + 0 comments

    public class Solution {

    private List<Integer> theList;
    private Scanner sc;
    
    public Solution(Scanner sc) {
    
        this.theList = new ArrayList<>();
        this.sc = sc;
    
        int sizeList = sc.nextInt();
        init(sizeList, sc);
    }
    
    private void init(int sizeList, Scanner sc) {
    
        if (sizeList > 0) {
    
            theList.add(sc.nextInt());
            init(sizeList - 1, sc);
        }
    }
    
    public void choose(String action){
    
        int index = sc.nextInt();
    
        if(action.equalsIgnoreCase("insert")){
    
            int element = sc.nextInt();
            theList.add(index, element);
    
        } else theList.remove(index);
    }
    
    public List<Integer> getList() {
    
        for (int element : theList) {
            System.out.print(element + " ");
        }
    
        return theList;
    }
    
    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
    
        Solution solution = 
            new Solution(sc);
    
        int queries = sc.nextInt();
    
        for(int i = 0; i < queries; i++){
    
            sc.nextLine();
            String action = sc.nextLine();
    
            solution.choose(action);
        }
    
        solution.getList();
    
    
    
    }  
    

    }