Пример #1
0
func doMergeSort(head *list.Node) ( //head != nil
	first *list.Node, last *list.Node) {
	first, last = head, list.FakeHead(&first)
	var size = 0
	for ; head != nil; size += 2 {
		if head.Next == nil {
			last = head
			size++
			break
		}
		var node0, node1 = head, head.Next
		head = node1.Next
		if node0.Val > node1.Val {
			last.Next, node1.Next, node0.Next = node1, node0, head
			last = node0
		} else {
			last = node1
		}
	}

	for step := 2; step < size; step *= 2 {
		head, last = first, list.FakeHead(&first)
		for head != nil {
			var left, right, node *list.Node
			left, head = head, cutPeice(head, step)
			right, head = head, cutPeice(head, step)

			last.Next, node = merge(left, right)
			last, node.Next = node, head
		}
	}
	return first, last
}
Пример #2
0
func stupidLinkList(size int) *list.Node {
	var head *list.Node
	var tail = list.FakeHead(&head)
	for i := 0; i < size; i++ {
		tail.Next = new(list.Node)
		tail = tail.Next
		tail.Val = i / 2
	}
	tail.Next = nil
	return head
}
Пример #3
0
func ramdomLinkList(size int) *list.Node {
	rand.Seed(time.Now().Unix())
	var head *list.Node
	var tail = list.FakeHead(&head)
	for i := 0; i < size; i++ {
		tail.Next = new(list.Node)
		tail = tail.Next
		tail.Val = rand.Int()
	}
	tail.Next = nil
	return head
}
Пример #4
0
func merge(left *list.Node, right *list.Node) (first *list.Node, last *list.Node) {
	first, last = nil, list.FakeHead(&first)
	for {
		last.Next = left
		if right == nil {
			break
		}
		for ; left != nil && left.Val <= right.Val; left = left.Next {
			last = left
		}
		last.Next = right
		if left == nil {
			break
		}
		for ; right != nil && left.Val > right.Val; right = right.Next {
			last = right
		}
	}
	for last.Next != nil {
		last = last.Next
	}
	return first, last
}