Leetcode 328. Odd Even Linked List-奇数位节点放前面,偶数位节点放后面,按照原来的顺序

Posted 二十六画生的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 328. Odd Even Linked List-奇数位节点放前面,偶数位节点放后面,按照原来的顺序相关的知识,希望对你有一定的参考价值。

Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.

The first node is considered odd, and the second node is even, and so on.

Note that the relative order inside both the even and odd groups should remain as it was in the input.

You must solve the problem in O(1) extra space complexity and O(n) time complexity.

Example 1:

Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]

Example 2:

Input: head = [2,1,3,5,6,4,7]
Output: [2,3,6,7,1,5,4]

Constraints:

  • n == number of nodes in the linked list
  • 0 <= n <= 104
  • -106 <= Node.val <= 106
package com.linkedlist;

/**
 * @Author you guess
 * @Date 2022/4/5 17:47
 * @Version 1.0
 * @Desc 1 2 3 4 5 6 7 -> 1 3 5 7 2 4 6
 * 奇数位节点放前面,偶数位节点放后面,按照原来的顺序
 */
public class Leetcode_328_OddEvenLinkedList 


    /**
     * 如果该位是奇数位,则插入到上次最新奇数位的后面,并指向第一个偶数位
     * 如果该位是偶数位,则插入到尾部即可
     * <p>
     * Runtime: 0 ms, faster than 100.00% of Java online submissions for Odd Even Linked List.
     * Memory Usage: 41.6 MB, less than 93.52% of Java online submissions for Odd Even Linked List.
     *
     * @param head
     * @return
     */
    public ListNode oddEvenList(ListNode head) 
        if (head == null || head.next == null || head.next.next == null) 
            return head;
        
        ListNode ji = head;
        ListNode ou = head.next;
        ListNode firstOu = head.next;//新的奇数位一直指向第一个偶数位
        ListNode cur = head.next.next;
        ListNode post = null;
        int index = 3;
        while (cur != null) 
            post = cur.next;//需要提前拿到
            ou.next = null;//防止出现2->3,3->2,死循环
            //ji.next=null;//会导致Wrong Answer,本来设置好了ji.next = firstOu;,这又断开了,导致结果全是奇数位节点无偶数位节点
            if (index % 2 == 1) 
                ji.next = cur;
                ji = ji.next;//ji与cur指向同一个节点
                ji.next = firstOu;//这里实际上也是cur.next = firstOu;则是3->2,但期望是3->4才对,所以前面提前用post = cur.next;
             else 
                ou.next = cur;
                ou = ou.next;
            
            index++;
            cur = post;
        
        return head;
    


    public void printListNode(ListNode head) 
        while (head != null) 
            System.out.println(head.val);
            head = head.next;
        
    

    public static void main(String[] args) 
        ListNode node7 = new ListNode(7, null);
        ListNode node6 = new ListNode(6, node7);
        ListNode node5 = new ListNode(5, node6);
        ListNode node4 = new ListNode(4, node5);
        ListNode node3 = new ListNode(3, node4);
        ListNode node2 = new ListNode(2, node3);
        ListNode node1 = new ListNode(1, node2);
        Leetcode_328_OddEvenLinkedList main = new Leetcode_328_OddEvenLinkedList();
        main.printListNode(main.oddEvenList(node1));
    

 odd 3个字母 是奇数,even 4个字母 是偶数。

以上是关于Leetcode 328. Odd Even Linked List-奇数位节点放前面,偶数位节点放后面,按照原来的顺序的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-328. Odd Even Linked List

Leetcode 328. Odd Even Linked List

LeetCode OJ 328Odd Even Linked List

LeetCode OJ 328Odd Even Linked List

leetcode328 Odd Even Linked List

<LeetCode OJ> 328. Odd Even Linked List