Insert a node at a specific position in a linked list

  • + 0 comments

    JAVA Collections Framwork import java.io.; import java.util.;

    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. */
        LinkedList<Integer> ll=new LinkedList<Integer>();
        Scanner s=new Scanner(System.in);
        Integer n=s.nextInt();
        Integer a,pos;
        for(Integer i=0;i<n;i++){
        a=s.nextInt();        
        ll.addLast(a);
        }
        a=s.nextInt();
        pos=s.nextInt();
        ll.add(pos,a);
        for(Integer j=0;j<ll.size();j++)
            System.out.print(ll.get(j)+" ");    
    }
    

    }