*Insertion Sort List

mac2022-06-30  83

Sort a linked list using insertion sort.

1 public class Solution { 2 public ListNode insertionSortList(ListNode head) { 3 ListNode dummy = new ListNode(0); 4 5 while (head != null) { 6 ListNode node = dummy; 7 while (node.next != null && node.next.val < head.val) { 8 node = node.next; 9 } 10 ListNode temp = head.next; 11 head.next = node.next; 12 node.next = head; 13 head = temp; 14 } 15 16 return dummy.next; 17 } 18 }

 

转载于:https://www.cnblogs.com/hygeia/p/5062374.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)