【问题标题】:Copying alternative nodes from one single linked list to another in java在java中将替代节点从一个单链表复制到另一个链表
【发布时间】:2019-10-25 12:16:58
【问题描述】:

我正在尝试编写一个名为 copyAlternate 的方法,它从单个链表中复制备用元素并将它们放入另一个作为方法中的参数发送的单个链表中

例如:如果第一个单链表是(4,6,10,12,2) 该方法应该生成一个带有 (4,10,2) 的单链表

这是我的代码:

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;
    }

我从方法不是 (4,10,2) 中只得到了 4 个 那么我的代码有什么问题?

【问题讨论】:

  • 如果不同时查看 SingleLinkedList 和 Node 类,很难更正代码。但有一点是肯定的,你不需要另一个 ArrayList,你可以直接将替代节点从一个列表复制到另一个。

标签: java generics singly-linked-list


【解决方案1】:

如果你需要,这里有一个完整的例子,尽量不要完全复制它,而是研究它并理解它在做什么:

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;
    }
}

【讨论】:

    【解决方案2】:

    实现它的另一种方式。希望它会有所帮助。

    // 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();
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      相关资源
      最近更新 更多