题解 | #划分链表#

划分链表

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-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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