Java 如何在已排序链表的开头插入节点?
在下面的代码中,我试图在链表的开头插入一个节点 当我尝试插入一个值2时,它应该被插入到开始节点之前的LL的开始处,因为它是最小的值Java 如何在已排序链表的开头插入节点?,java,data-structures,Java,Data Structures,在下面的代码中,我试图在链表的开头插入一个节点 当我尝试插入一个值2时,它应该被插入到开始节点之前的LL的开始处,因为它是最小的值 类主{ 公共静态void main(字符串参数[]){ 类节点{ int数据; 节点下一步; 节点(int数据){ 这个数据=数据; } 节点(int数据,节点下一个){ 这个。数据=数据; this.next=next; } 公共节点插入(节点开始,int x){ 如果(start==null | | start.data>x){ 开始=新节点(x,start.n
类主{
公共静态void main(字符串参数[]){
类节点{
int数据;
节点下一步;
节点(int数据){
这个数据=数据;
}
节点(int数据,节点下一个){
这个。数据=数据;
this.next=next;
}
公共节点插入(节点开始,int x){
如果(start==null | | start.data>x){
开始=新节点(x,start.next);
返回启动;
}
节点p=开始;
while(p.next!=null){
如果(p.next.data>x)
打破
p=p.next;}
p=p.next=新节点(x,p.next);
返回启动;
}
}
节点开始=新节点(10);
节点p=开始;
对于(int i=0;i而言,这方面的典型解决方案是需要一个围绕节点的类,并维护对列表头部的引用:
public class SortedLinkedList<T extends Comparable<T>> {
private Node head = null;
private class Node {
private final T value;
private Node next;
private Node(T value, Node next) {
this.value = value;
this.next = next;
}
public void insert(T value) {
assert value.compareTo(this.value) >= 0;
if (next == null || value.compareTo(next.value) <= 0)
next = new Node(value, next);
else
next.insert(value);
}
}
public void add(T value) {
if (head == null || value.compareTo(head.value) <= 0)
head = new Node(value, head);
else
head.insert(value);
}
public Stream<T> stream() {
return Stream.iterate(head, n -> n != null, n -> n.next)
.map(n -> n.value);
}
}
公共类分类链接列表{
私有节点头=null;
私有类节点{
私人最终T值;
私有节点下一步;
专用节点(T值,节点下一个){
这个值=值;
this.next=next;
}
公共空白插入(T值){
断言value.compareTo(this.value)>=0;
if(next==null | | value.compareTo(next.value)n.next)
.map(n->n.value);
}
}
或者,您可以使用一个“虚拟”头节点。可以说没有那么优雅,但确实避免了一些代码重复。下面是这个替代方案(为了更好地度量,将递归更改为迭代)
公共类分类链接列表{
私有最终节点头=新节点(null,null);
私有类节点{
私人最终T值;
私有节点下一步;
专用节点(T值,节点下一个){
这个值=值;
this.next=next;
}
}
公共无效添加(T值){
节点前一个=头;
while(previous.next!=null&&previous.next.value.compareTo(value)<0)
上一个=上一个。下一个;
previous.next=新节点(值,previous.next);
}
公共流(){
返回Stream.iterate(head.next,n->n!=null,n->n.next)
.map(n->n.value);
}
}
我对我的代码进行了上述更正,似乎没有在前面插入节点,尽管它比开始节点小。我指的是Schaum outlines的数据结构和算法java@KenniethMenezes我添加了一个完整的解决方案
2
10
20
30
40
public class SortedLinkedList<T extends Comparable<T>> {
private Node head = null;
private class Node {
private final T value;
private Node next;
private Node(T value, Node next) {
this.value = value;
this.next = next;
}
public void insert(T value) {
assert value.compareTo(this.value) >= 0;
if (next == null || value.compareTo(next.value) <= 0)
next = new Node(value, next);
else
next.insert(value);
}
}
public void add(T value) {
if (head == null || value.compareTo(head.value) <= 0)
head = new Node(value, head);
else
head.insert(value);
}
public Stream<T> stream() {
return Stream.iterate(head, n -> n != null, n -> n.next)
.map(n -> n.value);
}
}
public class SortedLinkedList<T extends Comparable<T>> {
private final Node head = new Node(null, null);
private class Node {
private final T value;
private Node next;
private Node(T value, Node next) {
this.value = value;
this.next = next;
}
}
public void add(T value) {
Node previous = head;
while (previous.next != null && previous.next.value.compareTo(value) < 0)
previous = previous.next;
previous.next = new Node(value, previous.next);
}
public Stream<T> stream() {
return Stream.iterate(head.next, n -> n != null, n -> n.next)
.map(n -> n.value);
}
}