今天在学递归的时候遇到一个跟指针有关的问题:
若p=head.next,当head.next=null时,p为何不为null,同理当head.next=head.next.next时,p的value不等于head.next.next的value;而当head.next.value改变时,p的vaule跟着改变。
原代码如下:
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// 1->3->5->7->9
// 1<-3<-5<-7<-9
Node head = new Node(1);
head.next = new Node(3);
head.next.next = new Node(5);
head.next.next.next = new Node(7);
head.next.next.next.next = new Node(9);
head = reverse(head);
while (head != null) {
System.out.print(head.value + " ");
head = head.next;
}
}
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value =data;
}
}
public static Node reverse(Node head) {
if(head.next == null) return head;
Node newHead =reverse(head.next);
Node p = head.next;
p.next = head;
head.next = null;//问题所在点,p!=null
return newHead;
}
}
自己试着改一下程序,将p指向head.next(即3所在结点),之后修改head.next指向的结点,发现p不为所动:
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// 1->3->5->7->9
Node head = new Node(1);
head.next = new Node(3);
head.next.next = new Node(5);
head.next.next.next = new Node(7);
head.next.next.next.next = new Node(9);
Node n=head;
while (n!= null) {
System.out.print(n.value+" ");
n = n.next;
}
System.out.println();
Node p = head.next;//p.value=3
System.out.println("—————————令p指向3所在结点———————————");
System.out.println("p.value="+p.value);
System.out.println("head.next.value="+head.next.value);
head.next = head.next.next ;
System.out.println("修改后 p.value="+p.value);
System.out.println("修改后 head.next.value="+head.next.value);
}
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
}
此时输出如下:
1 3 5 7 9
—————————令p指向3所在结点———————————
p.value=3
head.next.value=3
修改后 p.value=3
修改后 head.next.value=5
如果修改head.next指向的结点的value,发现p的value会因其而变:
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// 1->3->5->7->9
Node head = new Node(1);
head.next = new Node(3);
head.next.next = new Node(5);
head.next.next.next = new Node(7);
head.next.next.next.next = new Node(9);
Node n=head;
while (n!= null) {
System.out.print(n.value+" ");
n = n.next;
}
System.out.println();
Node p = head.next;//p.value=3
System.out.println("—————————令p指向3所在结点———————————");
System.out.println("p.value="+p.value);
System.out.println("head.next.value="+head.next.value);
head.next.value = 100 ;
System.out.println("修改后 p.value="+p.value);
System.out.println("修改后 head.next.value="+head.next.value);
n=head;
System.out.println("——————————重新输出链表情况——————————");
while (n!= null) {
System.out.print(n.value+" ");
n = n.next;
}
}
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
}
输出结果如下:
1 3 5 7 9
—————————令p指向3所在结点———————————
p.value=3
head.next.value=3
修改后 p.value=100
修改后 head.next.value=100
——————————重新输出链表情况——————————
1 100 5 7 9
思考分析:
p、head、head.next、head.next.next都是不同的结点,假设为结点A、B、C、D。
一开始B->C->D,A=C。
当B->D时,C还在,不会因改变而使C=D,此时仍有A=C,C->D。
如果C的数据改变,则A的数据跟着变化,即A.value =C.value=B.next.vaule。
理解有待提高,还望路过的大佬指点一二。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)