Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SLLJoinLists {
- public static void removeDuplicates(SLL<Integer> list){
- SLLNode<Integer> tmp=list.getFirst();
- SLLNode<Integer> next=null;
- while(tmp!=null){
- next=tmp.succ;
- if(next==null)
- break;
- if(tmp.element==next.element){
- list.delete(tmp);
- }
- tmp=next;
- }
- System.out.println(list);
- }
- public static void join(SLL<Integer> list1, SLL<Integer> list2){
- SLLNode<Integer> tmp1=list1.getFirst();
- SLLNode<Integer> tmp2=list2.getFirst();
- SLL<Integer> result=new SLL<>();
- while(tmp1!=null && tmp2!=null){
- if (tmp1.element<=tmp2.element){
- result.insertLast(tmp1.element);
- tmp1=tmp1.succ;
- }
- else{
- result.insertLast(tmp2.element);
- tmp2=tmp2.succ;
- }
- }
- if(tmp1!=null){
- while(tmp1!=null){
- result.insertLast(tmp1.element);
- tmp1=tmp1.succ;
- }
- }
- if(tmp2!=null){
- while(tmp2!=null){
- result.insertLast(tmp2.element);
- tmp2=tmp2.succ;
- }
- }
- System.out.println(result);
- removeDuplicates(result);
- }
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- int n1=in.nextInt();
- SLL<Integer> list1=new SLL<>();
- for(int i=0;i<n1;i++){
- list1.insertLast(in.nextInt());
- }
- int n2=in.nextInt();
- SLL<Integer> list2=new SLL<>();
- for(int i=0;i<n2;i++){
- list2.insertLast(in.nextInt());
- }
- join(list1,list2);
- }
- }
Add Comment
Please, Sign In to add comment