给定两个链接列表,找到一个列表是另一个列表的子列表的索引

时间:2015-10-01 10:10:36

标签: java algorithm data-structures

最近我参加了我大学的java编码挑战,并被问到这个我发现很难实现的问题。

问题是要实现给出两个detect的方法LinkedList,返回索引,其中第二个列表是第一个子列表。

detect((1,2,3),(2,3)) should return 1

列表的节点结构是

LinkedListNode {
   String val;
   LinkedListNode *next;
}

和方法签名

static int detect(LinkedListNode list, LinkedListNode sublist)

解决此问题的基本算法是什么?我是数据结构的新手。

4 个答案:

答案 0 :(得分:4)

我相信Collections.indexOfSubList实现了这一点。你可以看一下它的实现。 基本上是:

    ListIterator<?> si = source.listIterator();
    nextCand:
        for (int candidate = 0; candidate <= maxCandidate; candidate++) {
            ListIterator<?> ti = target.listIterator();
            for (int i=0; i<targetSize; i++) {
                if (!eq(ti.next(), si.next())) {
                    // Back up source iterator to next candidate
                    for (int j=0; j<i; j++)
                        si.previous();
                    continue nextCand;
                }
            }
            return candidate;
        }

答案 1 :(得分:1)

基本思想是遍历第二个列表,并且对于此列表中的每个索引,检查连续元素的第一个列表中的相等性。以下算法适用于您:

public static void main(String[] args) {
        List<Integer> list1 = new LinkedList<Integer>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        list1.add(5);
        list1.add(6);
        List<Integer> list2 = new LinkedList<Integer>();
        list2.add(2);
        list2.add(3);


        boolean contains = true;
        int currIndex = 0;
        int i = 0,j = 0;
        for(;j<list2.size();j++) {
            int e2 = list2.get(j);
            for(i=currIndex;i<list1.size();i++) {
                if(e2 == list1.get(i)) {
                    break;
                }
            }
            if(i == list1.size()) {
                contains = false;
                break;
            }
            currIndex++;
            if( contains && (currIndex == list2.size()) ) {
                System.out.println("Index is: " + (i-j));
            }
        }
    }

按预期打印Index is: 1

答案 2 :(得分:0)

static int detect(LinkedListNode list, LinkedListNode sublist) {
        int counter = 0;
        int index = -1;
        LinkedListNode sub = sublist;

        do {
            if (list.val == sub.val) {
                if (index == -1)
                    index = counter;
                if (sub.next != null) {
                    sub = sub.next;
                    if (sub.next == null) {
                        return index;
                    }
                }
            } else {
                index = -1;
                sub = sublist;
            }
            list = list.next;
            counter++;

        } while (list.next != null);

        return index;
    }

答案 3 :(得分:0)

因为,这里的值是String: -

static int find(LinkedListNode list, LinkedListNode sublist) {
    String listString = convertLinkedListToString(list);
    String sublistString = convertLinkedListToString(sublist);
    return listString.indexOf(sublistString);
}

private static String convertLinkedListToString(LinkedListNode list) {
    String listAsString = "";
    while(list != null) {
        listAsString = listAsString + list.val;
        list = list.next;
    }
    return listAsString;
}