Код: Выделить всё
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
public class MyLinkedList {
private ListNode head;
private int size;
//inner class for ListNode
private class ListNode {
private Object data;
private ListNode next;
private ListNode(Object d) {
this.data = d;
this.next = null;
}
}
public MyLinkedList() {
this.head = new ListNode(null); //with a dummy head node
this.size = 0;
}
Код: Выделить всё
public static MyLinkedList interleave(MyLinkedList A, MyLinkedList B) {
MyLinkedList C = new MyLinkedList();
ListNode curA = A.head.next;
ListNode curB = B.head.next;
ListNode curC = C.head;
boolean grabFromA = true;
while (curA != null && curB != null) {
if (grabFromA) {
curC.next = new ListNode(curA.data);
curA = curA.next;
}
else {
curC.next = new ListNode(curB.data);
curB = curB.next;
}
curC = curC.next;
grabFromA = !grabFromA; // Flips grab from A
}
while (curA != null) {
curC.next = new ListNode(curA.data);
curA = curA.next;
curC = curC.next;
}
while (curB != null) {
curC.next = new ListNode(curB.data);
curB = curB.next;
curC = curC.next;
}
return C; //change this as you need.
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... ic-context
Мобильная версия