zoukankan      html  css  js  c++  java
  • 328. 奇偶链表





    方法一:

    class Solution(object):
        def oddEvenList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head:
                return head
            ji = head_ji = ListNode(-1)
            ou = head_ou = ListNode(-1)
            cur = head
            flag = 1
            while cur:
                # 奇数位节点
                if flag:
                    ji.next = cur
                    ji = ji.next
                    flag = 0
                # 偶数位节点
                else:
                    ou.next = cur
                    ou = ou.next
                    flag = 1
                cur = cur.next
            ou.next = None
            ji.next = head_ou.next
            return head_ji.next
    

    方法二:

    class Solution(object):
        def oddEvenList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head:
                return head
            odd = head
            even_head = even = head.next
            while odd.next and even.next:
                odd.next = odd.next.next
                even.next = even.next.next
                odd = odd.next
                even = even.next
            odd.next = even_head
            return head
    
  • 相关阅读:
    通知
    KVO详解
    KVC详解
    KVC/KVO总结
    结构体Struct
    检测文件(夹)大小
    NSFileHandle&&NSFileManage
    ***NSFileManager
    获取文件扩展名
    MySql数据库_03
  • 原文地址:https://www.cnblogs.com/panweiwei/p/12900145.html
Copyright © 2011-2022 走看看