Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package diff;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Iterator;
- class SLLNode<T> {
- protected T value;
- protected SLLNode<T> next;
- public SLLNode(T value) {
- this(value, null);
- }
- public SLLNode(T value, SLLNode<T> next) {
- this.value = value;
- this.next = next;
- }
- }
- class SLL<T> implements Iterable<T> {
- private SLLNode<T> first;
- private int len = 0;
- public SLL() {
- first = null;
- }
- public void insertFirst(T element) {
- SLLNode<T> newFirst = new SLLNode<T>(element, first);
- this.first = newFirst;
- }
- public void insertLast(T element) {
- SLLNode<T> newLast = new SLLNode<T>(element);
- SLLNode<T> last = getLast();
- if(last != null) {
- last.next = newLast;
- } else {
- insertFirst(element);
- }
- len++;
- }
- private SLLNode<T> getLast() {
- if(first != null) {
- SLLNode<T> t = first;
- while(t.next != null) {
- t = t.next;
- }
- return t;
- }
- return null;
- }
- public int length() {
- return len;
- }
- public void print() {
- SLLNode<T> t = first;
- while(t != null) {
- System.out.print(t.value);
- t = t.next;
- if(t != null)
- System.out.print(" ");
- }
- }
- @Override
- public Iterator<T> iterator() {
- return new Iterator<T>() {
- private SLLNode<T> currentNode = first;
- @Override
- public boolean hasNext() {
- return currentNode != null;
- }
- @Override
- public T next() {
- T value = currentNode.value;
- currentNode = currentNode.next;
- return value;
- }
- };
- }
- }
- public class SpecialSLLJoin {
- public static SLL<Integer> specialJoin(SLL<Integer> list1, SLL<Integer> list2) {
- SLL<Integer> newList = new SLL<Integer>();
- Iterator<Integer> l1i = list1.iterator();
- Iterator<Integer> l2i = list2.iterator();
- int l1l = list1.length();
- int l2l = list2.length();
- while(l1l >= 2&& l2l >= 2) {
- newList.insertLast(l1i.next());
- newList.insertLast(l1i.next());
- newList.insertLast(l2i.next());
- newList.insertLast(l2i.next());
- l1l -= 2;
- l2l -= 2;
- }
- while(l1l > 0) {
- newList.insertLast(l1i.next());
- l1l--;
- }
- while(l2l > 0) {
- newList.insertLast(l2i.next());
- l2l--;
- }
- return newList;
- }
- public static void main(String[] args) throws IOException{
- SLL<Integer> lista1 = new SLL<Integer>();
- SLL<Integer> lista2 = new SLL<Integer>();
- BufferedReader stdin = new BufferedReader(new InputStreamReader(
- System.in));
- String s = stdin.readLine();
- int N = Integer.parseInt(s);
- s = stdin.readLine();
- String[] pomniza = s.split(" ");
- for (int i = 0; i < N; i++) {
- lista1.insertLast(Integer.parseInt(pomniza[i]));
- }
- s = stdin.readLine();
- N = Integer.parseInt(s);
- s = stdin.readLine();
- pomniza = s.split(" ");
- for (int i = 0; i < N; i++) {
- lista2.insertLast(Integer.parseInt(pomniza[i]));
- }
- SLL<Integer> spoeni = specialJoin(lista1,lista2);
- spoeni.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement