两个链表集之间的并集和交集,java

两个链表集之间的并集和交集,java,java,linked-list,union,Java,Linked List,Union,我正在尝试将方法写入“union”,可以这样描述:如果A、B、C是集合,则其形式为C=A.union(B)。Union返回一个集合,该集合包含集合a和B中的所有元素,但只列出一次重复项 我的想法是遍历集合A并将其所有元素添加到并集,然后遍历集合B,如果集合B的元素已存在于并集中,则不要将其插入结果,否则将全部插入并集 对于像我这样的初学者来说,这很复杂,因为我想将所有3个列表都包含到方法中(这会导致一系列错误)。我已经在SLinkedList类中编写了一些方法来检查和添加元素,但是参数从节点获取

我正在尝试将方法写入“union”,可以这样描述:如果A、B、C是集合,则其形式为C=A.union(B)。Union返回一个集合,该集合包含集合a和B中的所有元素,但只列出一次重复项

我的想法是遍历集合A并将其所有元素添加到并集,然后遍历集合B,如果集合B的元素已存在于并集中,则不要将其插入结果,否则将全部插入并集

对于像我这样的初学者来说,这很复杂,因为我想将所有3个列表都包含到方法中(这会导致一系列错误)。我已经在SLinkedList类中编写了一些方法来检查和添加元素,但是参数从节点获取对象

/** Singly linked list .*/
public class SLinkedList {

  protected Node head;   // head node of the list
  protected int size;    // number of nodes in the list

  /** Default constructor that creates an empty list */
  public SLinkedList() {
    head = new Node(null, null); // create a dummy head
    size = 0; 

 // add last
  public void addLast(Object data) {
      Node cur = head;
      // find last node
      while (cur.getNext() != null) {
          cur = cur.getNext();
      }
      // cur refers to the last node
      cur.setNext(new Node(data, null));
      size++;
  } 

 // contain method to check existing elements
  public boolean contain (Object target) {
      boolean status = false;
      Node cursor;
      for (cursor = head; cursor != null; cursor = cursor.getNext()) {
          if (target.equals(cursor.getElement())) {
              status = true;
          }       
      }   
      return status;
  }

  public SLinkedList union (SLinkedList secondSet) {
      SLinkedList unionSet = new SLinkedList();
      secondSet = new SLinkedList();
      Node cursor;
      for(cursor = head.getNext(); cursor != null; cursor = cursor.getNext()) {
          unionSet.addLast(cursor.getElement());
          // traverse secondSet, if an element is existed in either set A or union
              // set, skip, else add to union set
          }
      }
    return unionSet;
  }


  }
节点类

/** Node of a singly linked list of strings. */
public class Node {

  private Object element;   // we assume elements are character strings
  private Node next;

  /** Creates a node with the given element and next node. */
  public Node(Object o, Node n) {
    element = o;
    next = n;
  }

  /** Returns the element of this node. */
  public Object getElement() { return element; }

  /** Returns the next node of this node. */
  public Node getNext() { return next; }

  // Modifier methods:
  /** Sets the element of this node. */
  public void setElement(Object newElem) { element = newElem; }

  /** Sets the next node of this node. */
  public void setNext(Node newNext) { next = newNext; }
}

*更新* 问题是,如果涉及第二个列表
公共SLinkedList union(SLinkedList secondSet)
,我应该使用什么语法遍历集合B并检查集合B的元素是否已存在于结果中,然后不要将其插入结果,否则插入。我是否需要为集合B创建一个节点并遍历它?在union方法之外,可能有一个比较方法来比较这两个集合


请帮忙。谢谢大家。

我不知道问题是什么。替换
SLinkedList unionSet=null到<代码>SLinkedList unionSet=新SLinkedList()谢谢,现在我需要检查第二套
SLinkedList unionSet = null; // need a new SLinkedList() here
Node cursor;
for(cursor = head.getNext(); cursor != null; cursor = cursor.getNext()) {
    unionSet.addLast(cursor.getElement()); // NPE because unionSet is null
}