コード例 #1
0
ファイル: q03.go プロジェクト: serdmanczyk/ccint-go
func DeleteNode(n *linkedlist.Node) {
	if n.Next != nil {
		n.Value = n.Next.Value
		n.Next = n.Next.Next
	}
	// This doesn't 'delete' node but
	// rather modifies its value to that of the
	// node in front of it, and deletes it.
	// Using this method we cannot delete
	// the node at the front of a singly linked list.
}
コード例 #2
0
ファイル: q04.go プロジェクト: serdmanczyk/ccint-go
func PartitionLinkedList(l *linkedlist.LinkedList, x int) {
	var ltroot, lessthan, gtroot, greaterthan *linkedlist.Node

	current := l.Root
	for current != nil {
		if current.Value < x {
			if ltroot == nil || lessthan == nil {
				ltroot = current
				lessthan = current
			} else {
				lessthan.Next = current
				lessthan = lessthan.Next
			}
		} else {
			if gtroot == nil || greaterthan == nil {
				gtroot = current
				greaterthan = current
			} else {
				greaterthan.Next = current
				greaterthan = greaterthan.Next
			}
		}

		current = current.Next
	}

	if l.Root != nil {
		if ltroot != nil && gtroot != nil {
			l.Root = ltroot
			l.Head = greaterthan
			lessthan.Next = gtroot
			greaterthan.Next = nil
		}
	}
}