首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查LinkedList是否为SubList

检查LinkedList是否为SubList
EN

Stack Overflow用户
提问于 2014-01-15 12:14:04
回答 1查看 1K关注 0票数 0
代码语言:javascript
复制
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;
    }
}

我需要编写函数public int subList (CharList list),该函数获取list并返回该列表存在的次数。例如,如果我的列表是a b c d a b g e,而作为参数接收的列表是a b,它将返回2. ,方法应该尽可能高效。

目前我的问题是,我不知道如何同时循环两个列表,以便比较值

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-15 12:29:43

同时循环两个列表:

代码语言:javascript
复制
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
}

对于完整的解决方案,您必须遍历列表一次,而另一个列表必须循环多次,以达到匹配的次数。让我们以您的例子为例,a b c d a b g ea b相比

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21137143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档