Results 1 to 6 of 6
- 03-11-2011, 12:58 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
Recursive Linked List method: RemoveAll
The problem is: Remove all occurrences of a value from a linked list recursively.
The method header is: private static Node removeAll(Node head, String value)
Node head is the head of the list, String value is the value to be checked for removal.
The code I have so far is:
private static Node removeAll(Node head, String value)
{
if(head == null) return null;
if(head.next == null)
{
if(head.value.equals(value))
return null;
else return head;
}
else
{
Node probe = head;
if (probe.value.equals(value))
{
head = head.next;
return removeAll(probe.next, value);
}
else
{
return removeAll(probe.next, value);
}
}
}
I think the right way to do this is that probe is supposed to find the spot to remove, and then I do head = head.next to actually remove it from the list. However, I am removing all elements but the last one when I run it. If anyone could tell me what I'm doing wrong I would appreciate it. Thanks.
- 03-11-2011, 01:25 AM #2
You are making it much harder than it needs to be. Here is one stategy:
Java Code:removeAll(Node head, String value) { if(head != null) { removeAll(head.next, value); // recursion all the way to the tail // delete nodes on the way back up if(....) { // delete node } } }
- 03-11-2011, 01:51 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
- 03-11-2011, 02:04 AM #4
Is the list single or double linked? The pseudocode I provided is for a double linked list.
- 03-11-2011, 02:17 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
- 03-11-2011, 02:51 AM #6
Since you have a single LinkedList what you have to do is build up a new List with the undesired Nodes omiitted.
Lets say we have the following list: 1,9,2,9,3 and we want to remove all the 9's. Your recursion takes you all the way to the tail, ie the 3 node. Since there are no more node you return the current node (3 node). Your recursion goes back one level to the 9 node. Since this is a node you want to omit just return the node you got from the recursive call (ie the 3 node). Now go back another level to the 2 node. You return the current node PLUS the node you got from the recursive call (ie the 3 node). Back another level to the 9 node which you want to omit etc etc
Similar Threads
-
Basic Circular Linked List - addFirst() method works improperly
By carlodelmundo in forum New To JavaReplies: 9Last Post: 11-04-2011, 03:09 AM -
How to access an element of a linked list inside another linked list?
By smtwtfs in forum New To JavaReplies: 4Last Post: 02-21-2011, 09:34 AM -
I need Help Using an "AddSorted" method to a Doubly Linked List
By Auxilium in forum Advanced JavaReplies: 12Last Post: 10-10-2010, 11:17 PM -
Revised Linked List printing method question
By CirKuT in forum New To JavaReplies: 7Last Post: 12-12-2008, 10:21 PM -
Linked List removeAll Help
By unc123w in forum New To JavaReplies: 13Last Post: 09-30-2008, 02:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks