LeetCode 328:奇偶链表 Odd Even Linked List

简介: ​给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。

​给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。

请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。

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.

示例 1:

输入: 1->2->3->4->5->NULL
输出: 1->3->5->2->4->NULL

示例 2:

输入: 2->1->3->5->6->4->7->NULL 
输出: 2->3->6->7->1->5->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 ...

解题思路:

这道题很简单,迭代链表,将该链表奇数位节点和偶数位节点分别取出分隔成两个链表,然后将奇偶两个链表连接起来组成新链表,返回头节点即可。

需要记录偶数位节点的第一个节点,因为这是偶数链表的头节点,最后拼接链表时要用奇数链表的尾节点连接该节点。

你可以定义一个 int 型数值 i 为 0,每次迭代链表时 i 值自增 1 (i++),并判断 i 值除以 2 的余数为奇偶( i%2 ),以此为根据判断该节点是添加到奇链表后还是偶链表后。缺点是每次都要给 i 做自增运算 求余运算和判断余数,这在链表很长时将会占用很长的时间。而且int型值上限为 2147483647 ,超过这个值需要额外考虑方法。

另外一种方法是以第一个奇偶节点开始,将奇节点指向偶节点的下一个节点(肯定是奇节点),然后刷新奇链表,此时奇节点指向新加入的节点;将偶节点指向奇节点的下一个节点(肯定是偶节点),然后刷新偶链表,此时偶节点指向新加入的节点;......以此类推直到遇到空节点。

Java:

class Solution {
    public ListNode oddEvenList(ListNode head) {
        if (head == null || head.next == null || head.next.next == null) return head;//如果该链表内节点数在两个及以下直接返回头节点
        ListNode tmp = head.next;//暂存偶节点的第一个
        ListNode odd = head;//奇节点的第一个
        ListNode even = head.next;//偶节点的第一个
        while (even != null && even.next != null) {//循环条件,偶节点遇空时结束
            odd.next = even.next;//奇节点指向偶节点的下一个节点
            odd = odd.next;//刷新奇链表指针
            even.next = odd.next;//偶节点指向奇节点的下一个节点
            even = even.next;//刷新偶链表指针
        }
        odd.next = tmp;//连接双链表
        return head;
    }
}

Python3:

class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        if not head or not head.next or not head.next.next: return head
        tmp = head.next
        odd, even = head, head.next
        while even and even.next:
            odd.next = even.next
            odd = odd.next
            even.next = odd.next
            even = even.next
        odd.next = tmp
        return head

欢迎关注公.众号一起学习:爱写Bug

目录
相关文章
|
1月前
|
算法
LeetCode刷题---21.合并两个有序链表(双指针)
LeetCode刷题---21.合并两个有序链表(双指针)
|
1月前
|
算法
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
|
1月前
|
存储
LeetCode刷题---817. 链表组件(哈希表)
LeetCode刷题---817. 链表组件(哈希表)
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
1月前
|
算法 测试技术
LeetCode刷题--- 430. 扁平化多级双向链表(深度优先搜索)
LeetCode刷题--- 430. 扁平化多级双向链表(深度优先搜索)
|
1月前
|
算法 安全 数据处理
LeetCode刷题---707. 设计链表(双向链表-带头尾双结点)
LeetCode刷题---707. 设计链表(双向链表-带头尾双结点)
|
10天前
【力扣】21. 合并两个有序链表
【力扣】21. 合并两个有序链表
|
1月前
|
存储 JavaScript
leetcode82. 删除排序链表中的重复元素 II
leetcode82. 删除排序链表中的重复元素 II
22 0
|
1月前
leetcode83. 删除排序链表中的重复元素
leetcode83. 删除排序链表中的重复元素
10 0
|
1月前
leetcode2807.在链表中插入最大公约数
leetcode2807.在链表中插入最大公约数
16 0