LeetCode 147 — Insertion Sort List(C++ Java Python)
2014-02-27 21:03
579 查看
题目:http://oj.leetcode.com/problems/insertion-sort-list/
Sort a linked list using insertion sort.
题目翻译:
使用插入排序对链表进行排序。
分析:
注意细节。
C++实现:
Java实现:
Python实现:
感谢阅读,欢迎评论!
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
感谢阅读,欢迎评论!
相关文章推荐
- LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
- [LeetCode]题解(python):147-Insertion Sort List
- LeetCode 148 — Sort List(C++ Java Python)
- Java for LeetCode 147 Insertion Sort List
- 【LEETCODE】147- Insertion Sort List [Python]
- 【LeetCode-面试算法经典-Java实现】【147-Insertion Sort List(链表插入排序)】
- leetcode 147 Insertion Sort List java 算法
- leetcode_c++:链表:Insertion Sort List(147)
- leetcode 【 Insertion Sort List 】 python 实现
- LeetCode(147) Insertion Sort List
- LeetCode 147 Insertion Sort List(链表插入排序)
- Leetcode[147]-Insertion Sort List
- [Leetcode] #147 Insertion Sort List
- LeetCode 147 Insertion Sort List
- leetcode_147题——Insertion Sort List(线性表,插入排序)
- [Leetcode]#147 Insertion Sort List
- leetcode-147 Insertion Sort List
- LeetCode 147 Insertion Sort List
- [Leetcode][JAVA] Insertion Sort List
- [leetcode]Insertion Sort List @ Python