# Thread: Can you help me with linked lists?!

1. Member
Join Date
Nov 2016
Posts
8
Rep Power
0

## Can you help me with linked lists?!

hey,

In my java course I got this question:

Write a Java function max2List that takes two lists list1 and list2 of the same size and returns a list, list3, that will contain the maximum of the data between the corresponding nodes of lists list1 and list2.
Java Code:
```package tut1;
class Node {
int data; Node next;
Node(){
}
Node(int d, Node n){ data = d;
next = n;
} }
class List {
Node header;
}```
and my solution code:

Java Code:
```package tut1;

public class ListNode {

public static void main(String[] args) {
List list1 = new List();
list1.header = new Node();
Node n1 = new Node(5, null);
Node n2 = new Node(7, n1);
Node n3 = new Node(3, n2);
list1.header.next = n3;

List list2 = new List();
list2.header = new Node();
Node n4 = new Node(5, null);
Node n5 = new Node(2, n4);
Node n6 = new Node(1, n5);
list2.header.next = n6;

List list3 = new List();
list3.header = new Node();

while(list1.header.next != null){
System.out.println(list1.header.data);
}

}
}```

The problem I get infinite loop, what is my mistake here, and how to solve the question?

Thanks
Last edited by marvix; 11-30-2016 at 02:06 PM.

2. ## Re: Can you help me with linked lists?!

problem I get infinite loop, what is my mistake here,
Add some print statements that print the values used inside the loop to show you what the problem is.
What value should end the loop? Why isn't the code getting that value?
Where is the code updating the value of the variable that whose value will end the loop?

Please edit the Node class's code and properly format the statements. There should be indentations and there should not be two }s on the same line.
Last edited by Norm; 11-30-2016 at 02:15 PM.

3. ## Re: Can you help me with linked lists?!

You keep checking list1.header.next for != null. Since the header never changes, the loop runs on forever. You need to get a variable that represents the current node, something like this (untested):
Java Code:
```Node n = list1.header;
while(n != null) {
System.out.println(n.data);
n = n.next;
}```
Edit: sorry Norm, I spoiled OP a bit :)

4. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Can you help me with linked lists?!

Your code doesn't match your requirements.
You have no method max2List that takes two lists (are we talking linked lists hence the Node class?) and returns a list.

5. Member
Join Date
Nov 2016
Posts
8
Rep Power
0

## Re: Can you help me with linked lists?!

@SurfMan

You right, but the output not right :
0
5
7

and it should be :
3
7
5

6. Member
Join Date
Nov 2016
Posts
8
Rep Power
0

## Re: Can you help me with linked lists?!

max2List is new method I need to right it .. didnot reach this stage yet ;)

7. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Can you help me with linked lists?!

I would have said that's the first stage, as it's the first thing in the requirements.

8. Member
Join Date
Nov 2016
Posts
8
Rep Power
0

## Re: Can you help me with linked lists?!

@Tolls
You right, but the first the stage to max2List is the loop.

9. Member
Join Date
Nov 2016
Posts
8
Rep Power
0

## Re: Can you help me with linked lists?!

Java Code:
```List list1 = new List();
Node x33 = new Node(0, null);
Node x22 = new Node(5, x33);
Node x11 = new Node(7, x22);
list1.header = new Node(3,x11);

List list2 = new List();
Node x3 = new Node(0, null);
Node x2 = new Node(5, x3);
Node x1 = new Node(2, x2);
list2.header = new Node(1,x1);```
I know this wrong, but the only way I get the right output, what you think?

10. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Can you help me with linked lists?!

No, what I mean is the first stage is to have the method.

11. Member
Join Date
Nov 2016
Posts
8
Rep Power
0

## Re: Can you help me with linked lists?!

I give it up ;(

Can show me how the code should be?

12. ## Re: Can you help me with linked lists?!

Can show me how the code should be?
Before trying to write the code, can you describe what the program needs to do to solve the problem?
Make a list of the steps the program should take and then work on the steps one at a time.
If you have a problem with a step, post the step and the problem you are having trying to write code for that step.

A first step in understanding the problem is to make two lists and show the third list that should come from those two lists.

13. ## Re: Can you help me with linked lists?!

Originally Posted by marvix
I give it up ;(

Can show me how the code should be?
That's the spirit: throwing your hands up in the air and beg for 'teh codez' as a helpless puppy.

kind regards,

Jos

14. ## Re: Can you help me with linked lists?!

For your future reference, when an assignment speaks about "write a function/method X, that takes a Y and a Z and returns a V", what they want to see is this:
Java Code:
```public V X(Y y, Z z) {
//do something with y and z and return an instance of V here
}```
So, for example: "Write a function "add" that takes two ints and returns the sum of the ints" would look like this:
Java Code:
```public int add(int first, int second) {
int sum = first + second;
return sum;
}```
Your case isn't different. You can apply the same logic that I described above on the first part of your assignement: "Write a Java function max2List that takes two lists list1 and list2 of the same size and returns a list"

After you created the method/function (in Java, the name "method" is prefrerred"), you need to focus on the last part of the assignment: "returns list3, that will contain the maximum of the data between the corresponding nodes of lists list1 and list2"

15. Member
Join Date
Nov 2016
Posts
8
Rep Power
0

## Re: Can you help me with linked lists?!

This my first code in java .. this difficult for me. However, the problem starts with creating the list, am always getting 0 with list, and that should not be there. Then, am stack in the loop that create list3, creating the node is fine, but then how to point it to first node that will be created later in the loop?

16. ## Re: Can you help me with linked lists?!

creating the list, am always getting 0
Ok. First work on debugging the list to see why it is a 0? Where does the expected value come from? Does that value get into the Node? Does the code that gets the value from the Node return it correctly?

Line 2:
Java Code:
`    Node x33 = new Node(0, null);`
sets a value of 0. Is that the 0 you are talking about? Why is that node put in the list if you don't want a 0 in the list?

Can you post the current version of the code so we can see what you have done?
Last edited by Norm; 12-01-2016 at 02:32 PM.

17. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Can you help me with linked lists?!

I know you have spent a lot of time on this but your first post said.

In my java course I got this question:

Write a Java function max2List that takes two lists list1 and list2 of the same size and returns a list, list3, that will contain the maximum of the data between the corresponding nodes of lists list1 and list2.
It says nothing about writing your own linked list classes. Just taking two lists and finding maximums.

Regards,
Jim

18. Member
Join Date
Nov 2016
Posts
8
Rep Power
0

## Re: Can you help me with linked lists?!

Yes, it not saying anything about writing linked, but I need to test the max2List method.

The constructor Node() is not part from the question code, I just added to test if I will have 0 in the list, and the last node data will most be Node(), unless the loop will not show the last node data.

19. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Can you help me with linked lists?!

You can do that by simply creating and populating two instances of LinkedList (see the API). Then pass them to your method and
do the appropriate comparisons. As you compare, simply populate a third list and then return it.

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•