【问题标题】:return a linked list of first n elements返回前 n 个元素的链表
【发布时间】:2013-11-18 16:43:11
【问题描述】:

好的,伙计们,我需要编写一个方法; MyLinkedList getFirst(int n) – 返回前 n 个元素的链表。如果列表为空或 n > size 返回 null。

我迷路了,我已经完成了添加,删除,添加到中间,打印一串元素等等的方法,但是这个让我卡住了..

到目前为止我只有:

public MyLinkedList<E> getFirst(int n) {
    if(n > size ) {
        return null;
    }
    Node<E> current = head;
    for (int i = 0; i == n; i++) {
        current.next = new Node<E>(e);
    }
}

我知道这段代码是非常错误的,但我能想到的只是在这个任务上工作了一段时间,我想我只是筋疲力尽了,哈哈

感谢所有帮助。

【问题讨论】:

  • 家庭作业?这是问错地方了。
  • 如果有人在提问之前真正尝试过自己解决问题,那么家庭作业很好。
  • 坦率地说,看起来您已经为此工作了“一段时间”。
  • "a while" 并不是特别指这个问题,这是我第一次使用链表,我还有很多其他方法要写,这只是我坚持的一个,谢谢.

标签: java linked-list return


【解决方案1】:
  1. 创建一个空列表
  2. 将头部添加到列表中
  3. 继续将下一个节点添加到列表中,直到您拥有前 n 个节点。

【讨论】:

  • 谢谢,我稍微更新了我的代码,这个方向是否正确?
  • 您的循环看起来不正确。在某些时候,您需要返回一个 MyLinkedList&lt;E&gt; 对象,我认为这是您自己编写的。
【解决方案2】:
 public MyLinkedList getFirstN(int n) {
    MyLinkedList firstNList=new MyLinkedList();//create an empty list
    if(n>size)
        firstNList= null;
    else {
        Node tmp=head; //initialise tmp Node to the head(beginning) of list
        for(int i=0;i<n;i++) {
            firstNList.add(tmp);//add current node to the end of list
            tmp=tmp.getNext();
        }
    }
    return firstNList;
}

实现add(Node node) 方法以将Node 附加到列表末尾。

【讨论】:

    【解决方案3】:

    您可以将其用作原型并继续进行任何操作

    public class Node {
        private int data;
        private Node next;
    
        public Node(int data) {
            this.data = data;
        }
    
        public int getData() {
            return data;
        }
    
        public void setData(int data) {
            this.data = data;
        }
    
        public Node getNext() {
            return next;
        }
    
        public void setNext(Node next) {
            this.next = next;
        }
    
        public Node(int data, Node next) {
            this.data = data;
            this.next = next;
        }
    
    }
    
    
    public class LinkedList {
        private Node start;
    
        public LinkedList() {
            start = null;
        }
    
        public void insert(int x) {
            if(start == null) {
                start = new Node(x, start);
            } else {  
                Node temp = start;
    
                while(temp.getNext() != null) {
                    temp = temp.getNext();
                }
                Node newNode = new Node(x,null);
                temp.setNext(newNode);
            }
        }
    
        public void getFirst() {
            if(start == null) {
                System.out.println("\n List is empty !!");
            }
            else  {
                Node temp = start;
                System.out.println("\n First Element is --->" + temp.getData());
            }
        }
    
    }
    
    public class MainClass {
        public static void main(String[] args) {
            LinkedList ll = new LinkedList();
    
            System.out.println("\n--- Inserting 100 ---\n");
            ll.insert(100);
    
            ll.insert(101);
            ll.insert(102);
            ll.insert(103);
    
            System.out.println("\n--- First Element ---\n");
            ll.getFirst();
    
        } 
    
    }
    

    【讨论】:

    猜你喜欢
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2017-03-03
    • 2023-01-10
    • 1970-01-01
    相关资源
    最近更新 更多