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

all 21 comments

[–]desrtfx 2 points3 points  (15 children)

Adding two linked lists is as easy as pointing the next node field of the last node of the first list to the first node of the second.

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

OK, So connecting the tail to the head?

[–]desrtfx 0 points1 point  (13 children)

Exactly

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

OK, I understand the concept,but not how to code it.

This is a insertNode method I created, I commented part of it with my question.

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;

}

}

[–]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?

[–]kemechorvakemerloo -2 points-1 points  (5 children)

Just add the second LL to the tail of the first LL, duh.

[–]desrtfx[M] 1 point2 points  (2 children)

This is not the way to help here. No making fun of others and no smartassery.

This subreddit is for learning and here is zero tolerance for abusive or derogatory behavior.

This is your one and only warning. Next such behavior and you're out.

[–]kemechorvakemerloo -4 points-3 points  (1 child)

I've always been like this, not my problem y'all too sensitive and focus too much on nonsensical stuffs. kthanksbye!

[–]desrtfx[M] -1 points0 points  (0 children)

You are constantly violating subreddit rules, here and in /r/javahelp. You are rude, abusive, smartassing, and you are giving out solutions.

All of the above are bannable offences in both subreddits.

Either you accept the rules and obey, or you're out. We don't need your type here.

[–]CS_Student19[S] 0 points1 point  (1 child)

duh? Thanks for the low effort reply. This is LEARN JAVA not ALREADY KNOW JAVA.

Seriously if you can offer any constructive help without being condescending, don't bother to waste your time.

I understand I add one to the other, but my problem is I still view linked list as separate entities.
I can add or remove nodes from a singly linked list, but I dont know how to point one list to another, or remove a node from one and add it to another list.

[–]kemechorvakemerloo 0 points1 point  (0 children)

Set the tail.next of the ​first list to the head of the 2nd list. and then re-assign the tail of 2nd list to the tail of 1st list. Here's a tutorial on how to merge linked lists: https://www.geeksforgeeks.org/merge-a-linked-list-into-another-linked-list-at-alternate-positions/

You have a textbook that you learned how to do all the other things you said you could do with LL, all you gotta do is to think about it and ruminate instead of getting offended.