0
public class CharNode {
    private char _data;
    private CharNode _next;
    public CharNode(char dat, CharNode n) {
        _data = dat;
        _next = n;
    }
    public char getData() {
       return _data;
    }
    public CharNode getNext() {
        return _next;
    }
    public void setData(char d) {
       _data = d;
    }
    public void setNext(CharNode node) {
       _next = node;
     }
}

public class CharList {
    private CharNode _head;
    public CharList( ) {
        _head = null;
    }
    public CharList (CharNode node) {
        _head = node;
    }

    public int subList(IntList list)
    {
        int count = 0;
        IntNode listNode = _head;
        IntNode otherListNode = list._head; ;

        while (listNode != null)
        {
            if (otherListNode == null)
                otherListNode = list._head;
            if (listNode.getValue() == otherListNode.getValue())
            {
                listNode = listNode.getNext();
                otherListNode = otherListNode.getNext();
                if (otherListNode == null)
                    count++;
            }
            else
            {
                listNode = listNode.getNext();
                otherListNode = list._head;
            }              
        }

        return count;
    }
}

I need to write function public int subList (CharList list) that gets list and returns how many times this list exist. For example if my list is a b c d a b g e and the list received as parameter is a b it will return 2. The method should be as efficient as possible.

Currently my problem is that I don't know how to loop over the two lists at the same time in order to compare the values

user2908206
  • 265
  • 3
  • 16

1 Answers1

1

To loop over the two lists at the same time:

public bool equalsSubList(CharNode other) {
  CharNode node1 = this;  
  CharNode node2 = other;  
  while (node1 != null && node2 != null) { 
    // compare data from both nodes
    node1 = node1.getNext();
    node2 = node2.getNext();
  }
  // return true if all nodes where compared and are equal
}

For the complete solution, you will have to loop over your list once, and the other list as many times as there are matches. Let's take your example, a b c d a b g e compared to a b:

other |   this
      |
a b   |   a b c d a b g e 
^     |   ^ (match a, go to next in both lists)
a b   |   a b c d a b g e   |
  ^   |     ^ (match a b, counter is 1, go to next this, restart other)
a b   |   a b c d a b g e
^     |       ^ (does not match a, go to next this, restart other)
a b   |   a b c d a b g e
^     |         ^ (does not match a, go to next this, restart other)
a b   |   a b c d a b g e
^     |           ^ (match a, go to next in both lists)
a b   |   a b c d a b g e
  ^   |             ^ (match a b, counter is 2, go to next this, restart other)
a b   |   a b c d a b g e
^     |               ^ (does not match a, go to next this, restart other)
a b   |   a b c d a b g e
^     |                 ^ (does not match a, go to next this, restart other)
ericbn
  • 10,163
  • 3
  • 47
  • 55
  • thanks, in this case i will loop over list again and again, how can i loop over my list only once ? (O(n)) – user2908206 Jan 15 '14 at 12:43
  • Shouldn't it be `CharNode`, `CharList` and `getData()` instead of `IntNode`, `IntList` and `getValue()`? To check if your solution is good try debuging it with different test scenarios and compare the program with the desired behavior. Make sure you create unit tests to do this! – ericbn Jan 15 '14 at 13:37
  • Thanks, BTW my solution is O(n) ? – user2908206 Jan 15 '14 at 13:45
  • Yes, if you follow the behavior above, it will be O(n). http://stackoverflow.com/questions/1909307/what-does-on-mean – ericbn Jan 15 '14 at 13:56