【Leetcode】 328. Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.

Example:

Given 1->2->3->4->5->NULL,

return 1->3->5->2->4->NULL.

Note:

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

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

Tips:本题给定一个链表,要求将所有偶数位置的结点聚集在一起,连接在奇数位置的结点之后(调整数组顺序使奇数位于偶数前面)。

我的思路,另外建立一个奇数链表odd,以及一个偶数链表even 遍历原来的链表,奇数位置连接在odd之后,偶数位置连接在even之后。再将even记载odd最后一个节点之后,返回odd的头结点。

package medium;

import dataStructure.ListNode;

public class L328OddEvenLinkedList {
    // 空间复杂度为o(n)
    public ListNode oddEvenList(ListNode head) {
        // even 偶数 odd奇数
        if (head == null)
            return head;
        ListNode odd = new ListNode(0);
        ListNode odd1 =odd;
        // 先用0占一个头结点,之后直接越过去。
        ListNode even = new ListNode(0);
        ListNode even1 = even;
        int count = 1;
        ListNode cur=head;
        while (cur!= null) {
            if (count % 2 == 0) {
                even1.next = cur;
                System.out.println("even偶数"+even1.val);
                even1 = even1.next;
            } else {
                odd1.next = cur;
                System.out.println("odd奇数"+odd1.val);
                odd1 = odd1.next;
            }
            count++;
            cur = cur.next;
        }
        even1.next=null;
        odd1.next = even.next;
        return odd.next;
    }

    public static void main(String[] args) {
        ListNode head1 = new ListNode(1);
        ListNode head2 = new ListNode(2);
        ListNode head3 = new ListNode(3);
        ListNode head4 = new ListNode(4);
        ListNode head5 = new ListNode(5);
        ListNode head6 = new ListNode(6);
        ListNode head7 = new ListNode(7);
        ListNode head8 = new ListNode(8);
        ListNode head9 = new ListNode(9);
        head1.next = head2;
        head2.next = head3;
        head3.next = head4;
        head4.next = head5;
        head5.next = head6;
        head6.next = head7;
        head7.next = head8;
        head8.next = head9;
        head9.next = null;
        L328OddEvenLinkedList l32 = new L328OddEvenLinkedList();
        ListNode head = l32.oddEvenList(head1);
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }
    }
}

原文地址:https://www.cnblogs.com/yumiaomiao/p/8418403.html

时间: 2024-10-08 21:05:22

【Leetcode】 328. Odd Even Linked List的相关文章

<LeetCode OJ> 328. Odd Even Linked List

328. Odd Even Linked List My Submissions Question Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node numbe

【leetcode】Intersection of Two Linked Lists(easy)

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

【leetcode】Intersection of Two Linked Lists

Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes:

【Leetcode】Intersection of Two Linked Lists in JAVA

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 还是倒着想,把listnode放进栈里,然后一个一个pop出来,直到不一样的话,

白菜刷LeetCode记-328. Odd Even Linked List

发现简单题越来越少了,想偷懒都不可以了,今天的题目是中等难度的题目,题目如下: 这个题目是要根据链表的位置来修改链表,位置为奇数的节点全部排到前面,位置为偶数的节点全部排到奇数的后面,并且保持顺序不变. 想到的解决步骤为: 1.遍历数组,奇数的位置的节点组成一条新链表,偶数位置的节点组成另一个新链表: 2.将偶数链表接在奇数链表后面. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * function ListNode(val) { 4

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

【LeetCode】Linked List Cycle

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 做完Linked List Cycle II在做这题简直就是阉割版.. fast每次前进两步,slow每次前进一步,如果相遇则为true,否则false class Solution { public: bool hasCycle(ListNo

<LeetCode OJ> 328. Odd Even Linked List

328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in t

【Leetcode】Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 思路:由[Leetcode]Linked List Cycle可知,利用一快一慢两个指针能够判断出链表是否存在环路.假设两个指针相遇之前slow走了s步,则fast走了2s步,并且fast已经在长度