题解 | #划分链表#

划分链表

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
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 20:15
点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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