I'm revising Linked Lists and in the book I'm using they suggest the following code to search for a specific value:
public ListElement<Integer> find( ListElement<Integer> head, int data ){
ListElement<Integer> elem = head;
while( elem != null && elem.value() != data ){
elem = elem.next();
}
return elem;
}
But, cannot we iterate on head directly?
You can - but then it would be a somewhat misleading piece of code. If I look at a variable called head, I'd expect it to be the head of a list - whereas if I do:
head = head.next();
... then head refers to something which isn't the head of the list. It's always worrying when a variable name implies something that isn't true. It would technically work, but it would be a bad idea.
I would personally write the code more like this:
public ListElement<Integer> find(ListElement<Integer> head, int data) {
for (ListElement<Integer> current = head;
current != null;
current = current.next()) {
if (current.value == data) {
return current;
}
}
// No entry found
return null;
}
That way the "not found" case is more naturally distinct from the "found" case - it would be easier to change it to throw an exception if the value isn't found, for example.
Related
I've recently encountered a problem of iterating through the node from head to tail.
I usually used this method to iterate:
Node<E> walk = head;
while (walk != null) {
//do the job
walk = walk.getNext();
}
but when I tried to use for-loop instead, I got a null pointer error
for (Node<E> walk = head; walk.next != null; walk = walk.next) {
//do the job
}
Do I only have to use while-loop to iterate through the node? Or is it just my code making a bug because of my mistake?
Also, what makes a difference between walk.next and walk.getNext? I thought they are the same things
here is a getNext method
public Node<E> getNext() { return next; }
Below code should work because in your code you are iterating till walk==null
that is the reason you are getting null when accessing walk.next means null.next so Please follow below code.Hope it helps.
for (Node<E> walk = head; walk != null; walk = walk.next) {
//do the job
}
I'm preparing for interviews and wrote up this simple function for recursively reversing a singly linked list. The first node is a sentinel node, head. The following code works fine for: list.reverse(list.head.next), but I can't seem to get it to work if I just pass it head.
public Node<T> reverse(Node<T> current)
{
if (current == null)
return head;
if (current.next == null)
{
head.next = current;
return current;
}
reverse(current.next).next = current;
current.next = null;
return current;
}
I assume it doesn't work when I pass it head instead of head.next because I say current.next = null, but even if I check if current == head or if current.data == null and only use current.next = null when those aren't true, it still doesn't work. I'm sure there's a really simple fix, but I'm just not seeing it right now.
The above if passed head returns an empty list, and if the suggested changes are made, simply doesn't finish running, but I don't get any sort of error.
(EDITED)
I kind of get your problem now:
Simply speaking, the sentinel head acts simply as a pointer to the first node, instead of being part of the linked list. Therefore it will not be involved in the reverse process, and need to handle separately.
which means, the original list looks like:
HEAD -> a -> b -> c -> null
after reverse, it should look like
HEAD -> c -> b -> a -> null
In brief, it should look like (assume your code already works when passing in head.next)
public Node<T> reverse(Node<T> current)
{
if (current == head) {
return reverse(current.next);
}
// rest of your original code.
}
Just a further suggestion:
Your reverse() method, as an public instance method of your list class, shouldn't accept the current node, as it is conceptually meaningless for caller.
I believe you should make this method protected, which means something like:
public void reverse() {
this.head = reverseInternal(head);
}
private Node<T> reverseInternal(Node<T> node) {
// your original reverse logic
}
With such encapsulation, you don't even need to struggle before how to make your reverse works when you pass in the sentinel head: you can simply call reverseInternal(head.next) in your public reverse() method.
First: if it returns an empty list it doesn't "work".
There is no need for head to be an empty node. You should normally just keep the first node (in your case list.head.next) as your list.head. head should be a reference to where the list starts, not a separate node.
The reason your code empties the list when you pass it list.head is it sets list.head.next to null. This is because you assume the node you pass to the list is a regular one, while your head node is special.
Here's a solution for your assumptions (I'll assume someone insisted on this bizarre detached head thing. Just don't do it if you're designing the list yourself. Please...)
public Node<T> reverse(Node<T> current)
{
if (current == null)
return head;
if (current.next == null)
{
head.next = current;
return current;
}
Node<T> temp = current.next;
current.next = null;
head.next = temp;
reverse(temp).next = current;
return current;
}
Explanation: This still sets the last node's next to null, but it pushes the list's head one spot down as it runs through the list, eventually pointing it to the last (now first) member.
This reeks of homework.
But still.
In general:
f(Node<T> current, ...) {
f(current.next, ...);
}
For a list a > b > c > d > e sitting in the midle at d, one
probably has built c > b > a already, so guess what is needed as additional parameter to f?
Good luck.
After comments:
public Node<T> reverse(Node<T> current)
{
return reverseRec(current, null);
}
/**
* #param current to-do (sub-)list.
* #param resultDone done reversed list sofar.
* #return reversed result.
*/
public Node<T> reverseRecursively(Node<T> current, Node<T> resultDone)
{
if (current == null) {
return resultDone;
}
Node<T> next = current.next;
current.next = resultDone;
return reverseRecursively(next, current);
}
I have developed a code to merge two already sorted linked lists in java.
I need help with the following:
How do I retain the value of head node of merged list without using tempNode?
Can this code be better optimized?
public static ListNode mergeSortedListIteration(ListNode nodeA, ListNode nodeB) {
ListNode mergedNode ;
ListNode tempNode ;
if (nodeA == null) {
return nodeB;
}
if (nodeB == null) {
return nodeA;
}
if ( nodeA.getValue() < nodeB.getValue())
{
mergedNode = nodeA;
nodeA = nodeA.getNext();
}
else
{
mergedNode = nodeB;
nodeB = nodeB.getNext();
}
tempNode = mergedNode;
while (nodeA != null && nodeB != null)
{
if ( nodeA.getValue() < nodeB.getValue())
{
mergedNode.setNext(nodeA);
nodeA = nodeA.getNext();
}
else
{
mergedNode.setNext(nodeB);
nodeB = nodeB.getNext();
}
mergedNode = mergedNode.getNext();
}
if (nodeA != null)
{
mergedNode.setNext(nodeA);
}
if (nodeB != null)
{
mergedNode.setNext(nodeB);
}
return tempNode;
}
1: You have to keep a record of the first node, which means you will have to store it in a variable such as tempNode.
2: No. There's not much to optimize here. The process is quite trivial.
There are a few possibilities:
1) Instead of using mergedNode to keep track of the previous node, use nodeA.getNext().getValue() and nodeB.getNext().getValue(). Your algorithm will become more complex and you will have to deal with a few edge cases, but it is possible to eliminate one of your variables.
2) Use a doubly linked-list, and then use either nodeA.getPrev().getValue() and nodeB.getPrev().getValue() instead of mergedNode. You will also have to deal with edge cases here too.
In order to deal with edge cases, you will have to guarantee that your references can not possibly be null before calling getPrev(), getNext() or getValue(), or else you will throw an exception.
Note that the above modifications sacrifice execution time slightly and (more importantly) simplicity in order to eliminate a variable. Any gains would be marginal, and developer time is far more important than shaving a microsecond or two off of your operation.
I am trying to write a java class CircularList which contains the usual Node inner class and the instance variables:
OK, I won't give you the full implementation of the class, but instead I'll give you some advices.
You don't need to hold a reference to the last element. Place a Prev and Next reference to your nodes and your last node will be first.Prev
Besides the fact that circular lists have no end, they are quite the same than regular lists, but where you find the last item like this:
Node tmp = first;
while ( tmp.Next != null )
tmp = tmp.Next;
In a circular list the idea is like this:
Node tmp = first;
while ( tmp.Next != first )
tmp = tmp.Next;
Because you will never find a node pointing to null, unless the list is empty. One final advice, if you need to implement an indexer, remember that in a circular list there is no such thing as index out of range, because
list[count] = list[0] = list[count * k]
Keep that in mind, so calculating your index for those methods can be quite tricky. For positive indexes, the main idea is:
index = index % count;
For negative ones is slightly different. I hope I can help you with my words. If you want an implementations, I believe that there should be a few if you ask Google politely :)
Best of luck!
I'm not sure what your issue is, you seem to have everything needed. The only difference between that linked list and a normal one would be adding to the end.
In a regular linked list you would create a new node and make the last element point to that node. In this case, you change the pointer in the •Last node to point to the new node, and make the new node point to •First.
Removal works in the same way as a normal linked list. Whenever you want to remove a node, you find which other node points to that one (either previous, or in case of removing the first, check •Last) and make it point to wherever the one being deleted was pointing.
If this does not resolve your issue let me know and I'll try to help.
Just noticed someone has already asked the exact same question:
Can I use java.util.LinkedList to construct a circular/cyclic linked list?
Hint... circular list should have next and previous instead of first and last. The logic matters
class Node {
int value;
Node next;
Node prev;
Node(int initialValue) {
value = initialValue;
next = null;
prev = null;
}
public int getValue() {
return this.value;
}
}
class NodeList {
Node pointer;
NodeList() {
pointer = null;
}
public void insertNode(int nodeValue) {
Node newNode = new Node(nodeValue);
if( pointer == null ) {
newNode.next = newNode;
newNode.prev = newNode;
}else if( pointer.next == null && pointer.prev == null && pointer != null ) {
newNode.next = pointer;
newNode.prev = pointer;
pointer.prev = newNode;
pointer.next = newNode;
}
else if( pointer != null ) {
newNode.next = pointer.next;
newNode.prev = pointer;
pointer.next.prev = newNode;
pointer.next = newNode;
}
pointer = newNode;
System.out.println(“Successfully inserted : ” + pointer.getValue());
}
public void printRing( boolean direction ) {
Node tempNode = pointer;
do {
System.out.println( “Value = ” + tempNode.getValue() );
tempNode = direction ? tempNode.next : tempNode.prev;
} while( tempNode.value != pointer.value );
}
}
This is for homework but please know that I have looked online for help (such as http://www.sethi.org/classes/class_stuff/cis435/others/notes-java/data/collections/lists/simple-linked-list.html) and my textbook but I am still having some issues.
Any help would be appreciated...
Right now I'm trying to just insert values in but nothing is working. Whether it's the first item, whether it's being added as the last one, or somewhere in between.
Node header = null; // First element of list.
Node back = null; // Last element of list.
public void insert(int i, double value){ //insert value before i-th element
Node e = new Node();
e.num = value;
Node curr = header;
for(int x=0;x<i;x++) {
if (i == 1) { //we want to insert as first thing
if (size == 0) { //its the FIRST time we add something
header.next = e;
e.next = back;
break;
} else if (size == 1){
e.next = header.next; //i.e. the second thing in the list
header.next = e;
break;
} else {
e.next = header.next.next; //i.e. the second thing in the list
header.next = e;
break;
}
}
else if (x == (i-1)) {
e.next = curr.next;
curr.next = e;
break;
}
curr = curr.next;
}
size = size+1;
}
Not really sure why it isn't working.
Thanks!
For some reason, people who are still learning to program make things far more complicated then they need to be. I did it when I was learning java, I still do it when I am just getting into a new language, and students that I have marked find new and amazing ways to do it. You have more going on in your insert then there needs to be, for example, a method that inserts a value at a specific index should not check if it's the first item to be inserted (not saying it shouldn't check bounds). Here is the pseudo code of what I would do.
insert(index, value)
if index>size
throw null pointer
traverse to index -1 //lets call this nodeI
create newnode and set value
set newnode.next to nodeI.next
set nodeI.next to newnode
increase size.
Couple of handy hints for you, you should have a function to get an element from the link list, something that returns a node? public node elementAt(int index) for example? use that to traverse the linked list. If you want to append to the Linked list, try this
append(value)
insert(size-1,value)
and if you want to insert at the beginning? same idea
insert(value)
insert(0,value)
In the line e.next = header.next.next what would happen if header.next points to a 'null'? Is it possible to get there?
What are the corner cases you have to deal with and have you taken them all into account?
Can you start with the simplest case first, adding either an element to the front or an element to the back? Then use those functions to implement the insert?
A few suggestions:
implement java.util.List
Think about generics
Read this.
Start with "insert at the end" before you think about "insert at i".
I have tried a simple program, which will be useful for you guys, I am also learning Java, please bear with me for any mistakes, but this program works fine.
I am posting a very simple singly linked list program in Java, which I tried out today.
I hope it will help all.
LinkList.java
class LinkList
{
public static void main(String args[])
{
Node node = new Node(1);
node.addAtLast(2);
node.addAtLast(3);
node.addAtLast(4);
node.addAtLast(5);
node.printList();
}
}
Node.java
class Node
{
private int data;
private Node link;
public Node(int mydata)
{
data = mydata;
link = null;
}
public void printList()
{
System.out.print("|"+data+"|"+"->");
if(link != null)
{
//recursive call
link.printList();
}
else
{
//marking end of list as NULL
System.out.print("|NULL|");
}
}
public void addAtLast(int mydata)
{
if(link == null)
{
link = new Node(mydata);
}
else
{
link.addAtLast(mydata);
}
}
}
OUTPUT :
The below is our output
|1|->|2|->|3|->|4|->|5|->|NULL|