Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lectures;
- public class Prac1 {
- public static void main(String[] args) {
- SLList la = new SLList();
- SLList lb = new SLList();
- //la = {5,3,1}
- la.add(1);
- la.add(3);
- la.add(5);
- //lb = {8,7,6,4,2}
- lb.add(2);
- lb.add(4);
- lb.add(6);
- lb.add(7);
- lb.add(8);
- SLList lr = new SLList();
- lr.add(1);
- lr.add(2);
- lr.add(3);
- lr.add(4);
- lr.add(5);
- lr.add(6);
- la.llreverse(); //{1,3,5}
- lb.llreverse(); //{2,4,6,7,8}
- lr.llreverse();
- //list should be {1,2,3,4,5,6,7,8}
- interleaf(la,lb).printAll();
- }
- /**
- *
- * @param l1 {1,3,5}
- * @param l2 {2,4,6,7,8}
- * @return should be {1,2,3,4,5,6,7,8}
- */
- public static SLList interleaf(SLList l1, SLList l2) {
- SLList interList = new SLList(); //our final result
- //determines if any of list is empty as each iteration we
- //go through the head is removed
- while(!l1.isEmpty() && !l2.isEmpty()) {
- interList.add(l1.head.info);
- l1.deleteHead();//after add, l1.head.info is removed
- interList.add(l2.head.info);
- l2.deleteHead();//after add, l2.head.info is removed
- }
- /**if lists are uneven in size, we will put the
- remaining larger list in our interList
- */
- while(!l1.isEmpty()) {
- interList.add(l1.head.info);
- l1.deleteHead();
- }
- while(!l2.isEmpty()) {
- interList.add(l2.head.info);
- l2.deleteHead();
- }
- //right now its backwards, so we need to reverse it
- interList.llreverse();
- return interList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement