zoukankan      html  css  js  c++  java
  • [leetcode]Partition List @ Python

    原题地址:https://oj.leetcode.com/problems/partition-list/

    题意:

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

    You should preserve the original relative order of the nodes in each of the two partitions.

    For example,
    Given 1->4->3->2->5->2 and x = 3,
    return 1->2->2->4->3->5.

    解题思路:解决链表问题时,最好加一个头结点,问题会比较好解决。对这道题来说,创建两个头结点head1和head2,head1这条链表是小于x值的节点的链表,head2链表是大于等于x值的节点的链表,然后将head2链表链接到head链表的尾部即可。

    代码:

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        # @param head, a ListNode
        # @param x, an integer
        # @return a ListNode
        def partition(self, head, x):
            head1 = ListNode(0)
            head2 = ListNode(0)
            Tmp = head
            phead1 = head1
            phead2 = head2
            while Tmp:
                if Tmp.val < x:
                    phead1.next = Tmp
                    Tmp = Tmp.next
                    phead1 = phead1.next
                    phead1.next = None
                else:
                    phead2.next = Tmp
                    Tmp = Tmp.next
                    phead2 = phead2.next
                    phead2.next = None
            phead1.next = head2.next
            head = head1.next
            return head
  • 相关阅读:
    《显示器件应用分析精粹》构思
    《三极管应用分析精粹》已经交稿
    leetcode
    mskitten
    如果IBM再给我一次实习机会
    “完美工作”是什么样子
    一起四十岁退休吧……
    未来公司的酒会
    热泪盈眶的五十岁 | James Altucher
    一个程序员的辞呈
  • 原文地址:https://www.cnblogs.com/zuoyuan/p/3783276.html
Copyright © 2011-2022 走看看