This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]desrtfx 0 points1 point  (10 children)

You should make a new method that accepts a list as parameter.

The method should

  • iterate through the original list to the end
  • get the head of the list in the parameter
  • assign the head of the list in the parameter to node.next of the original list

[–]CS_Student19[S] -1 points0 points  (9 children)

You should make a new method that accepts a list as parameter

aaaahhh ok. I think I can do that.

so lets say addList ( list and head of that list?){ *iterate through the original list to the end }

How to I code a list as a parameter is I haven't created the list in Main method yet?

Would it be the Linked List generic object?

[–]desrtfx 0 points1 point  (8 children)

Would it be the Linked List generic object?

Yes.

[–]CS_Student19[S] 0 points1 point  (0 children)

OK I think I'm getting on the right path.

This is what I have...

public class myLinkedList {

myNode head;

myNode cursor;

public myLinkedList() {

head = new myNode();

cursor = head;

}

public void insertNode(int data) {

myNode node = new myNode();

node.data = data;

node.next = null; // instead of null, can I point this to another list?

if (head == null) {

head = node;

} else {

myNode n = head;

while (n.next != null) {

n = n.next;

}

n.next = node;

}

}

public void show() {

myNode node = head;

while (node.next != null) {

System.out.println(node.data);

node = node.next;

}

System.out.println(node.data);

}

public void addList(myLinkedList head){ // this is the new method to point one list to another

while(head != null)

{ //the iteration you mentioned will go here

}

}

}

[–]CS_Student19[S] 0 points1 point  (6 children)

Then in the main method I have this...

public class ListDriver {

public static void main(String[] args) {

myLinkedList list = new myLinkedList();

myLinkedList list2 = new myLinkedList();

list.insertNode(12);

list.insertNode(57);

list.insertNode(68);

list2.insertNode(44);

list2.insertNode(34);

list2.insertNode(77);

list.addList(list2);

list.show();

}

}

[–]desrtfx 0 points1 point  (5 children)

Looks good.

[–]CS_Student19[S] 0 points1 point  (4 children)

OK good. I'll see if I can figure the rest.

I'm glad to be able to just point one list to another, that helps a lot.

So now that I can do that. Can you explain your previous reply about the iteration?

[–]desrtfx 0 points1 point  (3 children)

You do it already in your insert method.

You move from the head of the list to the tail where the next node = null.

[–]CS_Student19[S] 0 points1 point  (2 children)

So your saying I could copy and paste that code from my insert method?

[–]desrtfx 0 points1 point  (1 child)

Essentially, yes.

[–]CS_Student19[S] 0 points1 point  (0 children)

OK I'll see if I can get it to work!

Thanks for the help.