原文:

给定和两个索引ab ,任务是打印一个以a开始并以b结尾的子列表。

示例

输入list = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null, a = 3, b = 9

输出3 4 5 6 7 8 9

输入list = 1 -> 3 -> 4 -> 10 -> null, a = 2, b = 2

输出:3

方法

请按照以下步骤解决问题:

  1. 创建三个实例变量:

    1. current:给定链表的头部

    2. subcurrent:子列表的头部

    3. subend:子列表的尾部。

  2. 遍历链表并初始化index_count变量,该变量在每次迭代后增加。

  3. index_count的值等于a时,将subcurrent设置为当前指向的节点。
  4. 如果index_count等于b,则将subend分配给当前引用的节点。 将subend.next指向null,表示子列表的末尾。
  5. subcurrentsubend打印列表内容。

下面的代码是上述方法的实现:

java

// java program to find the 
// sublist in a linked list 
import java.util.scanner; 
// class representing the 
// structure of a linked list 
public class linkedlistsublist { 
    node head; 
    class node { 
        int data; 
        node next = null; 
        node(int new_data) 
        { 
            data = new_data; 
        } 
    } 
    // function to push node 
    // at beginning of a 
    // linked list 
    public void pushnode(int new_data) 
    { 
        node new_node = new node(new_data); 
        new_node.next = head; 
        head = new_node; 
    } 
    // function to find sublist 
    public node sublist(node head, 
                        int a, 
                        int b) 
    { 
        node subcurrent = null; 
        node subend = null; 
        node current = head; 
        int i = 1; 
        // traverse between indices 
        while (current != null
               && i <= b) { 
            // if the starting index 
            // of the sublist is found 
            if (i == a) { 
                subcurrent = current; 
            } 
            // if the ending index of 
            // the sublist is found 
            if (i == b) { 
                subend = current; 
                subend.next = null; 
            } 
            // move to next node 
            current = current.next; 
            i  ; 
        } 
        // return the head 
        // of the sublist 
        return subcurrent; 
    } 
    // function to print 
    // the linked list 
    public void traversing() 
    { 
        node current = head; 
        while (current != null) { 
            system.out.print(current.data 
                               " -> "); 
            current = current.next; 
        } 
    } 
    // driver program 
    public static void main(string args[]) 
    { 
        linkedlistsublist list 
            = new linkedlistsublist(); 
        int n = 1; 
        int value = 10; 
        while (n < 11) { 
            list.pushnode(value--); 
            n  ; 
        } 
        // starting index 
        int a = 3; 
        // ending index 
        int b = 9; 
        list.head 
            = list.sublist( 
                list.head, a, b); 
        list.traversing(); 
    } 
} 

输出

3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 ->

时间复杂度o(n)

辅助空间o(1)



如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。