LeetCode 147 — Insertion Sort List(C++ Java Python)

题目:http://oj.leetcode.com/problems/insertion-sort-list/

Sort a linked list using insertion sort.

题目翻译:

使用插入排序对链表进行排序。

分析:

        注意细节。

C++实现:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *insertionSortList(ListNode *head) {
		if(head == NULL || head->next == NULL)
		{
			return head;
		}

		ListNode *node = head;
		ListNode *cur = NULL;

		while(node->next != NULL)
		{
			cur = node->next;

			if(cur->val >= node->val)
			{
				node = node->next;
				continue;
			}

			node->next = cur->next;

			if(cur->val <= head->val)
			{
				cur->next = head;
				head = cur;
				continue;
			}

			ListNode *pos = findInsertPos(head, cur);

			cur->next = pos->next;
			pos->next = cur;
		}

		return head;
	}

	ListNode *findInsertPos(ListNode *head, ListNode *node)
	{
		ListNode *pos = head;
		while(head != NULL && head->val < node->val)
		{
			pos = head;
			head = head->next;
		}

		return pos;
	}
};

Java实现:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode insertionSortList(ListNode head) {
		if (head == null || head.next == null) {
			return head;
		}

		ListNode node = head;
        ListNode cur = null;
        
		while (node.next != null) {
			cur = node.next;
			
			if (cur.val >= node.val) {
				node = node.next;
				continue;
			}
			
			node.next = cur.next;  // delete the node

			if (cur.val <= head.val) {
				cur.next = head;
				head = cur;
				continue;
			}

			ListNode pos = findInsertPos(head, cur);

			cur.next = pos.next;
			pos.next = cur;
		}

		return head;
	}

	ListNode findInsertPos(ListNode head, ListNode node) {
		ListNode pos = head;
		while (head != null && head.val < node.val) {
			pos = head;
			head = head.next;
		}

		return pos;
	}
}

Python实现:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param head, a ListNode
    # @return a ListNode
    def insertionSortList(self, head):
        if head == None or head.next == None:
            return head
        
        node = head;
        
        while node.next != None:
            cur = node.next
            
            if cur.val >= node.val:
                node = node.next
                continue
            
            node.next = cur.next
            
            if cur.val <= head.val:
                cur.next = head
                head = cur
                continue
            
            pos = self.findInsertPos(head, cur) 
            
            cur.next = pos.next
            pos.next = cur
            
        return head
        
        
    def findInsertPos(self, head, node):
        pos = head
        while head != None and node.val > head.val:
            pos = head
            head = head.next
        
        return pos

        感谢阅读,欢迎评论!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值