【每日一题Java】关于结点与其next的问题

今天在学递归的时候遇到一个跟指针有关的问题:

若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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林月明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值