简体   繁体   English

在 java 中将备用节点从一个单链表复制到另一个链表

[英]Copying alternative nodes from one single linked list to another in java

I'm trying to write a method called copyAlternate which copy alternate elements from a single linked list and put them in another Single linked list sent as a parameter in the method我正在尝试编写一个名为 copyAlternate 的方法,该方法从单个链接列表中复制备用元素并将它们放入另一个作为方法中的参数发送的单个链接列表中

for example: if the first Single Linked List is( 4,6,10,12,2) the method should generate a single linked list with (4,10,2)例如:如果第一个单链表是(4,6,10,12,2),该方法应该生成一个单链表(4,10,2)

here is my code:这是我的代码:

public boolean copyAlternate(SingleLinkedList<E> list1)
{

        if(head==null)
            return false;

        Node <E> temp = head;
        ArrayList <E> a1 = new ArrayList<E>();

        while(temp!=null) {
            a1.add(temp.data);
            temp=temp.next;
        }

        Node<E> tmp1=list1.head;
        for(int i=0;i<a1.size();i=i+2){

            if(list1.head==null) {
                list1.head =new Node(a1.get(i));
                tmp1=head;
                size++;
            }
            else
            {

             tmp1.next=new Node(a1.get(i));
             size++;
             tmp1=tmp1.next;
            }
        }
        return true;
    }

I got only 4 from method not (4,10,2) so what is the problem with my code?我从方法 not (4,10,2) 中只得到了 4,那么我的代码有什么问题?

Here is a full example if you need it, try not to copy it entirely but study it and understand what it's doing:如果您需要,这是一个完整的示例,尽量不要完全复制它,而是研究它并了解它在做什么:

public class Main {

    public static void main(String[] args) {
        SingleLinkedList<Integer> list = new SingleLinkedList<>();
        // (4, 6, 10, 12, 2)
        list.add(4).add(6).add(10).add(12).add(2);
        SingleLinkedList<Integer> newList = copyAlternate(list);
        System.out.println(newList);
        // prints: (14, 10, 2)
    }

    public static class Node<T> {
        public Node<T> next;
        public T data;

        public Node() {}
        public Node(T data) {this.data = data;}
    }

    public static class SingleLinkedList<T> {
        private Node<T> head;
        private Node<T> tail;

        public SingleLinkedList<T> add(T data) {
            if (tail != null) {
                tail.next = new Node<>(data);
                tail = tail.next;
            } else {
                head = new Node<>(data);
                tail = head;
            }
            return this;
        }

        @Override public String toString() {
            StringBuilder sb = new StringBuilder();
            if (head != null) {
                sb.append(head.data);
                Node<T> curr = head.next;
                while (curr != null) {
                    sb.append(", ").append(curr.data);
                    curr = curr.next;
                }
            }
            return sb.toString();
        }
    }

    /**
     * Copies only the even index nodes (0, 2, 4..) into a new list and returns that list. 
     * Doesn't create a deep copy of data
     */
    public static <T> SingleLinkedList<T> copyAlternate(SingleLinkedList<T> list) {
        Objects.requireNonNull(list, "list");
        SingleLinkedList<T> newList = new SingleLinkedList<>();
        Node<T> other = list.head;
        if (other != null) {
            // copy other data to head of new list and remember that as the current node
            Node<T> curr = newList.head = new Node<>(other.data);
            // get the next next other node
            other = other.next;
            if (other != null) {
                other = other.next;
            }
            while (other != null) {
                // copy the data into the next node then remember that as the current node
                curr = curr.next = new Node<>(other.data);
                // get the next next other node
                other = other.next;
                if (other != null) {
                    other = other.next;
                }
            }
        }
        return newList;
    }
}

Another way you can achieve it.另一种方法可以实现它。 Hope it will help.希望它会有所帮助。

// define the structure of a single node
class ListNode<T>
{
   public T data;

   public ListNode<T> next;

   public ListNode(T data)
   {
      this.data = data;
      this.next = null;
   }
}

public class SingleLinkedListGenerics<T>
{
   private ListNode<T> head = null;

   private ListNode<T> tail = null;

   // used to insert a node at the end of linked list
   public void insertLast(T data)
   {
      ListNode<T> newNode = new ListNode<>(data);

      if (head == null)
      {
         head = tail = newNode;
      }
      else
      {
         tail.next = newNode;
         tail = newNode;
      }
   }

   // For printing Linked List
   public void displayList()
   {
      System.out.println("\nPrinting LinkedList (head --> last) ");
      ListNode<T> current = head;
      while (current != null)
      {
         System.out.println(current.data + ", ");
         current = current.next;
      }
   }

   public static <T> SingleLinkedListGenerics<T> copyAlternate(SingleLinkedListGenerics<T> list)
   {

      if (list.head == null)
         return null;

      SingleLinkedListGenerics<T> newList = new SingleLinkedListGenerics<>();

      ListNode<T> tmp = list.head;

      ListNode<T> newNode = new ListNode<>(tmp.data); // getting the first node
      newList.head = newNode;
      newList.tail = newNode;

      tmp = tmp.next;

      while (tmp != null && tmp.next != null) //
      {
         tmp = tmp.next; // skipping one node

         newList.insertLast(tmp.data);

         tmp = tmp.next;
      }

      return newList;
   }


   public static void main(String args[])
   {
      SingleLinkedListGenerics<Integer> myLinkedlist = new SingleLinkedListGenerics<>();
      myLinkedlist.insertLast(4);
      myLinkedlist.insertLast(6);
      myLinkedlist.insertLast(10);
      myLinkedlist.insertLast(12);
      myLinkedlist.insertLast(2);

      myLinkedlist.displayList();

      SingleLinkedListGenerics<Integer> myLinkedlist1 = copyAlternate(myLinkedlist);

      myLinkedlist1.displayList();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM