leetcode 328 奇偶链表

作者: Arsenal4ever | 来源:发表于2020-01-12 23:46 被阅读0次

    使用双指针啊!!!每个都往后走两步,最后拼在一起。

    # 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
            """
            if not head:
                return head
            p1, p2 = head, head.next
            result, t = p1, p2
            while p2 and p2.next:
                p1.next = p1.next.next
                p2.next = p2.next.next
                p1 = p1.next
                p2 = p2.next
            p1.next = t
            return result
    

    相关文章

      网友评论

        本文标题:leetcode 328 奇偶链表

        本文链接:https://www.haomeiwen.com/subject/kjzwactx.html