Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โปรแกรมจัดเรียง Linked List ให้เป็นลำดับน้อยไปมากใน Python


มี Linked List จัดเรียงให้เป็นลำดับน้อยไปมาก

เช่น [5,8,4,1,5,6,3] ให้ [1,3,4,5,5,6,8]

ขั้นตอนการแก้ปัญหา

  • เก็บค่าทุกโหนดใน list values
  • เรียง values
  • แปลงเป็น deque แล้วใส่ค่ากลับโหนดเดิมทีละตัว
  • คืนค่า head

ตัวอย่างโค้ด

import collections
class ListNode:
    def __init__(self, data, next=None):
        self.val = data; self.next = next

def make_list(elements):
    head = ListNode(elements[0])
    for e in elements[1:]:
        ptr = head
        while ptr.next: ptr = ptr.next
        ptr.next = ListNode(e)
    return head

def print_list(head):
    ptr = head; print('[', end='')
    while ptr: print(ptr.val, end=', '); ptr = ptr.next
    print(']')

class Solution:
    def solve(self, node):
        values = []
        head = node
        while node:
            values.append(node.val)
            node = node.next
        values.sort()
        values = collections.deque(values)
        node = head
        while node:
            node.val = values.popleft()
            node = node.next
        return head

ob = Solution()
head = make_list([5,8,4,1,5,6,3])
print_list(ob.solve(head))

อินพุต

[5, 8, 4, 1, 5, 6, 3]

เอาต์พุต

[1, 3, 4, 5, 5, 6, 8, ]