链表笔记
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package test; public class ListNode{ int val; ListNode next; public ListNode( int x) { val = x; } public ListNode() {} public static void main(String[] args) { int n = 2; ListNode l1 = new ListNode(1); ListNode l2 = new ListNode(2); ListNode l3 = new ListNode(3); ListNode l4 = new ListNode(4); ListNode l5 = new ListNode(5); ListNode head = l1; l1.next = l2; l2.next = l3; l3.next = l4; l4.next = l5; while (head != null ) { System. out .println(head.val); head = head.next; } ListNode h1 = new ListNode(); h1.next = head; //这里将 h1 指向了 head<br> ListNode first = h1; // 对 first 操作的时候, h1 和 head 也在改变。在内存三者共用同一块内存 first.next = l3; while (h1 != null ) { System. out .println(h1.val); h1 = h1.next; } while (head != null ) { System. out .println(head.val); head = head.next; } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步