Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public LinkedList() {

}


private static class Node<T> {
T element;
Node<T> next;
Expand Down Expand Up @@ -75,6 +76,15 @@ public void addFirst(E element) {
size++;
}

/**
* Appends the specified element to the end of this list.
*
* @param element element to be appended to this list.
*/
public void addLast(E element) {
add(element);
}


/**
* Inserts the specified element at the specified position in this list.
Expand Down Expand Up @@ -115,20 +125,27 @@ public void add(int index, E element) {
*
* @throws RuntimeException if the list is empty.
*/
public void remove() {
public E removeLast() {
if (size == 0)
throw new RuntimeException("linkList size should greater than or equal to 1");
E element;
Node<E> next = first.next;
if (next == null) {
element = first.element;

first = null;
} else {
Node<E> last = first;
while (last.next != null)
last = last.next;
last.prev.next = null;

element = last.element;

last = null; // help GC
}
size--;
return element;
}


Expand Down Expand Up @@ -163,6 +180,7 @@ public E removeFirst() {
return element;
}


/**
* Returns the element at the specified position in this list.
*
Expand Down Expand Up @@ -229,6 +247,232 @@ public String toString() {
return sb.toString();
}

/**
* 把该链表逆置
* 例如链表为 3->7->10 , 逆置后变为 10->7->3
*/
public void reverse() {
Node<E> next;
Node<E> current = first;
for (int i = 0; i < size; i++) {
next = current.next;
current.next = current.prev;
current.prev = next;
if (next != null) {
current = next;
}
}
first = current;
}

/**
* 删除一个单链表的前半部分
* 例如:list = 2->5->7->8 , 删除以后的值为 7->8
* 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10
*/
public void removeFirstHalf() {
int index = size() / 2;
Node<E> current = first;
Node<E> prev;
for (int i = 0; i < index; i++) {
prev = current;
current = current.next;
delete(prev);
}
current.prev = null;
first = current;
size = size - index;
}

/**
* 从第i个元素开始, 删除length 个元素 , 注意i从0开始
*
* @param i
* @param length
*/
public void remove(int i, int length) {
checkElementIndex(i);
if (length + i > size()) {
throw new IllegalArgumentException("Length + i should less than or equal " + size());
}
Node<E> head = first;
Node<E> tail = first;
if (i == 0 && length == size()) {
first = null;
} else if (i == 0 && length < size()) {
for (int j = 0; j < length; j++) {
head = head.next;
}
head.prev = null;
first = head;
} else {
for (int j = 0; j < i; j++) {
head = head.next;
}
head = head.prev;
for (int j = 0; j < length + i; j++) {
tail = tail.next;
}
head.next = tail;
if (tail != null) {
tail.prev = head;
}
}
size = size - length;
}


/**
* 假定当前链表和list均包含已升序排列的整数
* 从当前链表中取出那些list所指定的元素
* 例如当前链表 = 11->101->201->301->401->501->601->701
* listB = 1->3->4->6
* 返回的结果应该是[101,301,401,601]
*
* @param list
*/
public int[] getElements(LinkedList list) {
int[] newArray = new int[list.size()];
Node mapNode = list.first;
Node valueNode = this.first;
int indexOfList = 0;
int indexOfArray = 0;
while (mapNode != null && valueNode != null) {
int mapValue = (int) mapNode.element;
if (mapValue == indexOfList) {
newArray[indexOfArray] = (int) valueNode.element;
mapNode = mapNode.next;
valueNode = valueNode.next;
indexOfArray++;
} else {
mapNode = mapNode.next;
}
indexOfList++;
}
return newArray;
}

/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 从当前链表中中删除在listB中出现的元素
*
* @param list
*/

public void subtract(LinkedList<E> list) {
Node pNode = first;
Node qNode = list.first;
Node prev = null;
Node deletedNode;
while (pNode != null && qNode != null) {
if ((int) qNode.element < (int) pNode.element) {
qNode = qNode.next;
} else if ((int) qNode.element > (int) pNode.element) {
prev = pNode;
pNode = pNode.next;
} else {
if (prev == null) { // 头结点
first = pNode.next;
} else {
prev.next = pNode.next;
}
deletedNode = pNode;
pNode = pNode.next;
qNode = qNode.next;
delete(deletedNode);
size--;
}
}

}

private void delete(Node node) {
node.element = null;
node.prev = null;
node.next = null;
}

/**
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同)
*/
public void removeDuplicateValues() {
Node<E> current = first;
Node<E> next = current.next;
while (next != null) {
if (current.element == next.element) {
current.next = next.next;
if (next.next != null) {
next.next.prev = current;
}
delete(next);
next = current.next;
size--;
} else {
current = current.next;
next = next.next;
}

}
}

/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素)
*
* @param min
* @param max
*/
public void removeRange(int min, int max) {
Node current = first;
Node prev = null;
Node deletedNode;
while (current != null) {
if ((int) current.element >= max) {
break;
}
if ((int) current.element > min && (int) current.element < max) {
if (prev == null) { // 头结点
first = current.next;
} else {
prev.next = current.next;
}
deletedNode = current;
current = current.next;
delete(deletedNode); // help gc
size--;
} else {
prev = current;
current = current.next;
}
}
}

/**
* 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同)
* 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列
*
* @param list
*/
public LinkedList intersection(LinkedList list) {
LinkedList l = new LinkedList();
Node pNode = first;
Node qNode = list.first;
while (pNode != null && qNode != null) {
if ((int) pNode.element < (int) qNode.element) {
pNode = pNode.next;
} else if ((int) pNode.element > (int) qNode.element) {
qNode = qNode.next;
} else {
l.add(pNode.element);
pNode = pNode.next;
qNode = qNode.next;
}
}
return l;
}


/**
* Constructs an IndexOutOfBoundsException detail message.
* Of the many possible refactorings of the error handling code,
Expand Down
Loading