Write code to partition a linked list around a value x such that all nodes less than x come before all nodes greater than or equal to x.
void partitionList(List<T> list, int pivot) { int elementsSeen = 0; int size = list.size(); Iterator<T> iter = list.iterator(); while (elementsSeen < size && iter.hasNext()) { T element = iter.next(); iter.remove(); if (element < pivot) { list.add(0); } else { list.add(size - 1); } elementsSeen++; } }
No comments:
Post a Comment