1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| package main
import "fmt"
type ListNode struct { Val int Next *ListNode }
func createLinkedList(values []int) *ListNode { if len(values) == 0 { return nil } head := &ListNode{Val: values[0]} current := head for _, val := range values[1:] { current.Next = &ListNode{Val: val} current = current.Next } return head }
func printLinkedList(head *ListNode) string { var result []int for head != nil { result = append(result, head.Val) head = head.Next } return fmt.Sprintf("%v", result) }
func mergeTwoList(l1, l2 *ListNode) *ListNode { dummy := &ListNode{} p, p1, p2 := dummy, l1, l2 for p1 != nil && p2 != nil { if p1.Val <= p2.Val { p.Next = p1 p1 = p1.Next } else { p.Next = p2 p2 = p2.Next } p = p.Next }
if p1 != nil { p.Next = p1 } else if p2 != nil { p.Next = p2 }
return dummy.Next }
func sortList_0(head *ListNode) *ListNode { return sort(head, nil) }
func sort(head, tail *ListNode) *ListNode { if head == nil { return head } if head.Next == tail { head.Next = nil return head }
slow, fast := head, head for fast != tail { slow = slow.Next fast = fast.Next if fast != tail { fast = fast.Next } }
mid := slow return mergeTwoList(sort(head, mid), sort(mid, tail)); }
func sortList(head *ListNode) *ListNode { if head == nil { return head }
length := 0 for node := head; node != nil; node = node.Next { length ++ }
dummy := &ListNode{Next: head} for subLength := 1; subLength < length; subLength <<= 1 { prev, cur := dummy, dummy.Next for cur != nil { head1 := cur for i := 1; i < subLength && cur.Next != nil; i ++ { cur = cur.Next }
head2 := cur.Next cur.Next = nil cur = head2 for i := 1; i < subLength && cur != nil && cur.Next != nil; i ++ { cur = cur.Next }
var next *ListNode if cur != nil { next = cur.Next cur.Next = nil }
prev.Next = mergeTwoList(head1, head2) for prev.Next != nil { prev = prev.Next } cur = next } }
return dummy.Next }
func main() { testCases := []struct { values []int expected []int }{ { values: []int{4,2,1,3}, expected: []int{1,2,3,4}, }, { values: []int{-1,5,3,4,00}, expected: []int{-1,0,3,4,5}, }, { values: []int{}, expected: []int{}, }, }
for i, tc := range testCases { head := createLinkedList(tc.values) fmt.Printf("Test Case %d, Input: head = %v\n", i+1, printLinkedList(head)) result := sortList(head) resultStr := printLinkedList(result) expectedStr := fmt.Sprintf("%v", tc.expected) if resultStr == expectedStr { fmt.Printf("Test Case %d, Output: %v, PASS\n", i+1, resultStr) } else { fmt.Printf("Test Case %d, Output: %v, FAIL (Expected: %v)\n", i+1, resultStr, expectedStr) } } }
|