题解 | #划分链表#

划分链表

http://www.nowcoder.com/practice/1dc1036be38f45f19000e48abe00b12f

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

#常规的划分成两个子链表,最后两个子链表合并
# 
# @param head ListNode类 
# @param x int整型 
# @return ListNode类
#
class Solution:
    def partition(self , head , x ):
        # write code here
        left=ListNode(0)
        left_tail=left
        right=ListNode(0)
        right_tail=right
        while head:
            temp=head
            head=head.next
            temp.next=None
            if temp.val<x:
                left_tail.next=temp
                left_tail=temp
            else:
                right_tail.next=temp
                right_tail=temp
        left_tail.next=right.next
        return left.next
全部评论

相关推荐

02-25 13:02
中南大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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