Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- 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);
- }
- }
- private SLLNode<T> getLast() {
- if(first != null) {
- SLLNode<T> t = first;
- while(t.next != null) {
- t = t.next;
- }
- return t;
- }
- return null;
- }
- public SLL<Integer> joinLists(SLL<Integer> list) {
- SLL<Integer> newList = new SLL<Integer>();
- @SuppressWarnings("unchecked")
- SLLNode<Integer> l1curr = (SLLNode<Integer>) first;
- SLLNode<Integer> l2curr = list.first;
- Integer lastInserted = null;
- while(l1curr != null&&l2curr != null) {
- int newVal = l1curr.value;
- if(l1curr.value < l2curr.value) {
- l1curr = l1curr.next;
- } else if(l1curr.value > l2curr.value) {
- newVal = l2curr.value;
- l2curr = l2curr.next;
- } else {
- l1curr = l1curr.next;
- l2curr = l2curr.next;
- }
- if(lastInserted != null) {
- if(newVal == lastInserted) continue;
- }
- lastInserted = newVal;
- newList.insertLast(newVal);
- }
- while(l1curr != null) {
- int newVal = l1curr.value;
- l1curr = l1curr.next;
- if(lastInserted != null) {
- if(newVal == lastInserted) continue;
- }
- lastInserted = newVal;
- newList.insertLast(newVal);
- }
- while(l2curr != null) {
- int newVal = l2curr.value;
- l2curr = l2curr.next;
- if(lastInserted != null) {
- if(newVal == lastInserted) continue;
- }
- lastInserted = newVal;
- newList.insertLast(newVal);
- }
- return newList;
- }
- public void print() {
- SLLNode<T> t = first;
- while(t != null) {
- System.out.println(t.value);
- t = t.next;
- }
- }
- @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 SLLJoinLists {
- 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 = lista1.joinLists(lista2);
- Iterator<Integer> it = spoeni.iterator();
- while (it.hasNext()) {
- System.out.print(it.next());
- if(it.hasNext())
- System.out.print(" ");
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement