Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在链表中将所有偶数排列在奇数之前_Java - Fatal编程技术网

Java 如何在链表中将所有偶数排列在奇数之前

Java 如何在链表中将所有偶数排列在奇数之前,java,Java,给定一个链表,比如{1,2,3,5,6,11,10} 我需要输出为{2,6,10,1,3,5,11}。 偶数需要排在奇数之前。一种方法是创建一个新列表,然后在第一个列表中循环,将偶数添加到新列表的开头,将奇数添加到结尾。一种方法是创建一个新列表,然后在第一个列表中循环,将偶数添加到新列表的开头,将奇数添加到结尾。一个简单的解决方案是枚举列表的所有元素,并根据数字的奇数将它们分配到两个不同的列表,例如偶数列表和奇数列表。然后使用基本排序对每个列表进行单独排序,最后将两个列表连接成一个新列表。一个简

给定一个链表,比如{1,2,3,5,6,11,10} 我需要输出为{2,6,10,1,3,5,11}。
偶数需要排在奇数之前。

一种方法是创建一个新列表,然后在第一个列表中循环,将偶数添加到新列表的开头,将奇数添加到结尾。

一种方法是创建一个新列表,然后在第一个列表中循环,将偶数添加到新列表的开头,将奇数添加到结尾。

一个简单的解决方案是枚举列表的所有元素,并根据数字的奇数将它们分配到两个不同的列表,例如偶数列表和奇数列表。然后使用基本排序对每个列表进行单独排序,最后将两个列表连接成一个新列表。

一个简单的解决方案是枚举列表的所有元素,并根据数字的奇数将它们分配到两个不同的列表,例如偶数列表和奇数列表。然后使用基本排序分别对每个列表进行排序,最后将两个列表连接到一个新列表中。

我只需在列表中运行两次:

  • 首次通过evens输出
  • 第二次通过输出的赔率

这将是O(n),而使用比较器等可能不是。

我只需在列表中运行两次:

    LinkedList<Integer> list = new LinkedList<Integer>();
    LinkedList<Integer> newlist = new LinkedList<Integer>();
    int[] a = {1,2,3,5,6,11,10};
    for(int i=0;i<a.length;i++) {
        list.add(a[i]);
    }

    for(int i=0,j=0; i<list.size(); i++) {
        if(a[i]%2 == 0) {
            newlist.add(j++, a[i]);
        } else {
            newlist.addLast(a[i]);
        }
    }

    System.out.println(newlist);
  • 首次通过evens输出
  • 第二次通过输出的赔率
这将是O(n),使用比较器等可能不是。

LinkedList=new LinkedList();
    LinkedList<Integer> list = new LinkedList<Integer>();
    LinkedList<Integer> newlist = new LinkedList<Integer>();
    int[] a = {1,2,3,5,6,11,10};
    for(int i=0;i<a.length;i++) {
        list.add(a[i]);
    }

    for(int i=0,j=0; i<list.size(); i++) {
        if(a[i]%2 == 0) {
            newlist.add(j++, a[i]);
        } else {
            newlist.addLast(a[i]);
        }
    }

    System.out.println(newlist);
LinkedList newlist=新LinkedList(); int[]a={1,2,3,5,6,11,10}; 对于(inti=0;i
LinkedList=newlinkedlist();
LinkedList newlist=新LinkedList();
int[]a={1,2,3,5,6,11,10};

对于(int i=0;i我认为使用
ArrayList
更好,可以参考下面返回排序列表的方法

public static List<Integer> sortList(List<Integer> list){
    LinkedList<Integer> sortedList = new LinkedList<Integer>();

    List<Integer> evenList=new ArrayList<Integer>();
    List<Integer> oddList=new ArrayList<Integer>();
    for(Integer i:list){

        if(i%2==0)
            evenList.add(i);
        else
            oddList.add(i);
    }

    Collections.sort(evenList);
    Collections.sort(oddList);
    sortedList.addAll(evenList);
    sortedList.addAll(oddList);


    return sortedList;
}
公共静态列表排序列表(列表){
LinkedList sortedList=新建LinkedList();
List evenList=new ArrayList();
List oddList=new ArrayList();
for(整数i:列表){
如果(i%2==0)
增加(i);
其他的
增补(i);
}
集合。排序(evenList);
集合。排序(oddList);
sortedList.addAll(均匀列表);
sortedList.addAll(oddList);
返回分类列表;
}
输入:

[1,2,3,5,6,11,10]

输出:


[2,6,10,1,3,5,11]

为了更好地进行操作,我认为使用
ArrayList
,您可以参考下面返回排序列表的方法

public static List<Integer> sortList(List<Integer> list){
    LinkedList<Integer> sortedList = new LinkedList<Integer>();

    List<Integer> evenList=new ArrayList<Integer>();
    List<Integer> oddList=new ArrayList<Integer>();
    for(Integer i:list){

        if(i%2==0)
            evenList.add(i);
        else
            oddList.add(i);
    }

    Collections.sort(evenList);
    Collections.sort(oddList);
    sortedList.addAll(evenList);
    sortedList.addAll(oddList);


    return sortedList;
}
公共静态列表排序列表(列表){
LinkedList sortedList=新建LinkedList();
List evenList=new ArrayList();
List oddList=new ArrayList();
for(整数i:列表){
如果(i%2==0)
增加(i);
其他的
增补(i);
}
集合。排序(evenList);
集合。排序(oddList);
sortedList.addAll(均匀列表);
sortedList.addAll(oddList);
返回分类列表;
}
输入:

[1,2,3,5,6,11,10]

输出:

[2,6,10,1,3,5,11]
  • 在temp中使用头指针,并使用temp遍历列表
  • 一旦遇到正数,在头部创建一个新节点,复制数据并删除temp指向的节点。头部将指向新节点,temp将指向下一个节点
  • 遍历列表直到temp->next!=NULL
  • 由于创建和删除操作为O(1),时间复杂度将为O(n)
      • 在temp中使用头指针,并使用temp遍历列表
      • 一旦遇到正数,在头部创建一个新节点,复制数据并删除temp指向的节点。头部将指向新节点,temp将指向下一个节点
      • 遍历列表直到temp->next!=NULL
      • 由于创建和删除操作为O(1),时间复杂度将为O(n)

        • 以下是算法/实现

          public static LinearNode<Integer> seperateEvenAndOddNodes(LinearNode<Integer> head) {
              LinearNode<Integer> tail = head, prevNode=null, currNode = head, nextNode;
              int length = length(head), count = 0;
              boolean allEven = true, allOdd = true;
              //point to the last node, start dumping all nodes after this
              while (tail.next() != null) {
                  if (tail.getElement() % 2 == 0) {
                      allOdd = false;
                  } else {
                      allEven = false;
                  }
                  tail = tail.next();
              }
              // Dont do anything if either all odd or all even
              if (allOdd || allEven) {
                  return head;
              }
              // Make sure you don't go in infinite loop, and hence condition to make sure, you traverse limited nodes.
              while (currNode != null && count < length) {
                  nextNode = currNode.next();
                  //move currNode to the end of list, if it is odd.
                  if (currNode.getElement() % 2 == 1) {
                      LinearNode<Integer> temp = currNode;
                      if (prevNode != null) {
                          prevNode.next(nextNode);
                          currNode = prevNode;
                      } else {
                          head = nextNode;
                          currNode = null;
                      }
                      tail.next(temp);
                      tail = temp;
                      temp.next(null);
                  }
                  prevNode = currNode;
                  currNode = nextNode;
                  count++;
              }
              //return the new head, in case the list begins with odd
              return head;
          }
          
          public静态LinearNode分离even和odd节点(LinearNode头){
          LinearNode tail=head,prevNode=null,currNode=head,nextNode;
          整数长度=长度(头),计数=0;
          布尔allEven=true,allOdd=true;
          //指向最后一个节点,在此之后开始转储所有节点
          while(tail.next()!=null){
          if(tail.getElement()%2==0){
          allOdd=假;
          }否则{
          allEven=假;
          }
          tail=tail.next();
          }
          //不要做任何事情,无论是奇数还是偶数
          如果(允许){
          回流头;
          }
          //确保你们并没有进入无限循环,所以要确保你们遍历有限的节点。
          while(currNode!=null&&count
          下面是单元测试

          @Test
          public void seperateEvenAndOddNodesTest() {
              LinearNode<Integer> head = buildLinkedList(2,3,4,1,7,8,9);
              head = LinkedListUtil.seperateEvenAndOddNodes(head);
              assertLinkedList(head, 2,4,8,3,1,7,9);
          
              head = buildLinkedList(9,3,4,1,7,8,2);
              head = LinkedListUtil.seperateEvenAndOddNodes(head);
              assertLinkedList(head, 4,8,2,9,3,1,7);
          
          
              head = buildLinkedList(1,3,5,7);
              head = LinkedListUtil.seperateEvenAndOddNodes(head);
              assertLinkedList(head, 1,3,5,7);
          
              head = buildLinkedList(2,4,6,8);
              head = LinkedListUtil.seperateEvenAndOddNodes(head);
              assertLinkedList(head, 2,4,6,8);
          }
          
          @测试
          公共无效,分别为even和oddnodestest(){
          LinearNode头=buildLinkedList(2,3,4,1,7,8,9);
          head=LinkedListUtil.SeparateEvenandOddNodes(head);
          assertLinkedList(负责人,2,4,8,3,1,7,9);
          head=buildLinkedList(9,3,4,1,7,8,2);
          head=LinkedListUtil.SeparateEvenandOddNodes(head);
          assertLinkedList(负责人,4,8,2,9,3,1,7);
          head=buildLinkedList(1,3,5,7);
          head=LinkedListUtil.SeparateEvenandOddNodes(head);
          assertLinkedList(负责人,1,3,5,7);
          head=buildLinke