-8

所以我有一些 Set,我想将元素添加到 LinkedList 中,这样我就可以遍历它们。我将如何在java中执行此操作?

4

1 回答 1

0

取自这里的一个问题:将项目添加到链表的末尾

class Node {
    Object data;
    Node next;
    Node(Object d,Node n) {
        data = d ;
        next = n ;
       }

   public static Node addLast(Node header, Object x) {
       // save the reference to the header so we can return it.
       Node ret = header;
   // check base case, header is null.
   if (header == null) {
       return new Node(x, null);
   }

   // loop until we find the end of the list
   while ((header.next != null)) {
       header = header.next;
   }

   // set the new node to the Object x, next will be null.
   header.next = new Node(x, null);
   return ret;
   }
}
于 2013-10-19T03:01:56.130 回答