[leetcode147]insertion sort list
2017-05-08 15:08
471 查看
解题思路:两个指针,一个指针的下一个与另一个指针的下一个做交换操作,先给后拿的思想
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def insertionSortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return head
dummy=ListNode(0)
dummy.next=head
while head and head.next:
if head.val<=head.next.val:
head=head.next
else:
cur=dummy
while cur.next.val<head.next.val:
cur=cur.next
tmp=head.next
head.next=tmp.next
tmp.next=cur.next
cur.next=tmp
return dummy.next
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def insertionSortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return head
dummy=ListNode(0)
dummy.next=head
while head and head.next:
if head.val<=head.next.val:
head=head.next
else:
cur=dummy
while cur.next.val<head.next.val:
cur=cur.next
tmp=head.next
head.next=tmp.next
tmp.next=cur.next
cur.next=tmp
return dummy.next
相关文章推荐
- leetcode_147 Insertion Sort List
- LeetCode 147 Insertion Sort List
- LeetCode(147) Insertion Sort List
- LeetCode(147) Insertion Sort List
- [LeetCode-Algorithms-147] "Insertion Sort List" (2017.12.21-WEEK16)
- leetcode_c++:链表:Insertion Sort List(147)
- LeetCode147—Insertion Sort List
- 【leetcode】147 Insertion Sort List
- leetCode #147 Insertion Sort List
- [LeetCode]题解(python):147-Insertion Sort List
- LeetCode147 Insertion Sort List
- LeetCode: Insertion Sort List [147]
- leetcode 147 —— Insertion Sort List
- LeetCode 147 Insertion Sort List
- The Solution to LeetCode 147 Insertion Sort List
- Insertion Sort List - LeetCode 147
- [LeetCode147]Insertion Sort List(链表插入排序)
- [LeetCode 147] Insertion Sort List
- Leetcode[147]-Insertion Sort List
- leetcode147~Insertion Sort List