题解 | #链表的奇偶重排#

链表的奇偶重排

http://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3


# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @return ListNode类
#
class Solution:
    def oddEvenList(self , head ):
        # write code here
        if not head or not head.next or not head.next.next:
            return head
        left_head = ListNode(-1)
        left_tail = left_head
        right_head = ListNode(-1)
        right_tail = right_head
        temp = head
        while temp:
            new_temp = temp.next
            left_tail.next = temp
            left_tail = left_tail.next
            temp = new_temp
            if not temp :
                break
            new_temp = temp.next
            right_tail.next = temp
            right_tail = right_tail.next
            temp = new_temp
        left_tail.next = right_head.next
        right_tail.next = None
        return left_head.next
            
全部评论

相关推荐

点赞 评论 收藏
分享
06-26 17:24
已编辑
宁波大学 golang
迷失西雅图:别给,纯kpi,别问我为什么知道
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务