[leetcode] 328. Odd Even Linked List @ python

本文介绍了一种链表操作算法,旨在将链表中的奇数位置节点与偶数位置节点进行分离并重新组合。提供了三种不同的解决方案,包括转换为列表再重组、使用虚拟节点分别连接奇偶节点以及构建两个独立的链表再合并。每种方法都详细解释了其运行机制,并附带了Python代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题

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 1:

Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULL
Example 2:

Input: 2->1->3->5->6->4->7->NULL
Output: 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 …

解法1

将链表转化为list, 将列表的奇数和偶数元素分开, 然后组成新列表, 最后将列表转化为链表.
Time: O(n)
Space: O(n)

代码

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def oddEvenList(self, head: 'ListNode') -> 'ListNode':
        # get the list
        l = []
        while head:
            l.append(head.val)
            head = head.next
            
        l = l[::2] + l[1::2]        
        dummy = p = ListNode(0)        
        for val in l:
            p.next = ListNode(val)
            p = p.next
        return dummy.next

解法2

构造两个虚拟节点, 遍历head, 将odd连接奇数节点, even连接偶数节点, 退出循环后将两个链表连接起来, 最后返回新链表的头部节点.

Time: O(n)
Space: O(1)

代码

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def oddEvenList(self, head: 'ListNode') -> 'ListNode':
        d1 = odd = ListNode(0)        
        d2 = even = ListNode(0)
        while head:
            odd.next = head
            even.next = head.next
            odd = odd.next
            even = even.next
            head = head.next.next if even else None
        odd.next = d2.next
        return d1.next

解法3

构造两个链表odd和even代表奇数链表和偶数链表, 用i表示当前是奇数还是偶数节点, 遍历head链表, 将奇数节点放入odd, 偶数节点放入even. 这里注意, 最后的even.next必须为None才能通过代码测试. 最后将odd和even连接起来.

Time: O(n)
Space: O(1)

代码

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def oddEvenList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        d1 = odd = ListNode(0)
        d2 = even = ListNode(0)
        i = 1
        while head:
            if i%2:
                odd.next = head
                odd = odd.next
            else:
                even.next = head
                even = even.next
            i += 1
            head = head.next
        even.next = None
        odd.next = d2.next
        return d1.next
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值