链表:(单链表、双链表、循环链表)
优点:插入和删除效率高;空间灵活
缺点:不支持随机访问,查询效率较低
单链表特点:结点单向指引,即在当前结点情况下,无法知道上一结点,只可知道下一结点
单链表:
1 | public class SingLinkList<T> { |
应用:
有一个整数单链表L,设计一个尽可能高效的算法将所有负整数的元素移动到其他元素前面。例如,L=(1,2,-1,-2,3,-3,4),移动后 L=(-1,-2,-3,1,2,3,4)。
/**
* 将所有负整数的元素移动到其他元素前面,移动后不改变负数元素先后顺序
*
* @return
*/
public static void moveNum(SingLinkList<Integer> linkList) {
SingLinkListNode temp = linkList.head;
int i = 1;
if (linkList.head.next == null) return;
while (temp.next != null) {
if ((Integer)temp.next.date < 0) {
linkList.insert(i, (Integer) temp.next.date);
i++;
temp.next = temp.next.next;
}else {
temp = temp.next;
}
}
}
---
- 本文作者: zzr
- 本文链接: http://zzruei.github.io/2023/01482ed644.html
- 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!