单链表的反转 布满荆棘的人生 2023-01-13 10:53 116阅读 0赞 # 链表的反转 # * 代码: // A->B->C->D // D->C->B->A package com.weidd.best.dataStructure.linkedListTest; public class SingleLinkedListTest { static class Node { String data; Node next; public Node(String data) { this.data = data; } public Node next(Node next) { this.next = next; return next; } } public static void main(String[] args) { Node first = new Node("1"); Node second = new Node("2"); Node third = new Node("3"); Node fourth = new Node("4"); Node five = new Node("5"); first.next(second); second.next(third); third.next(fourth); fourth.next(five); five.next(null); Node node=first; while (first != null) { System.out.print(first.data); first = first.next; System.out.print("-->"); } System.out.println(); System.out.println("--------链表反转---------");// 链表反转 Node cur =node,pre=null,temp=null; while(cur!=null){ temp=cur.next; cur.next=pre; pre=cur; cur=temp; } while(pre!=null){ System.out.print(pre.data); pre=pre.next; System.out.print("->"); } } }
相关 单链表反转 常常面试我们会遇到考察列表反转的问题 如 1-->2-->3-->4-->5-->null这样的一个链表进行反转成为 null<--1<--2<--3<--4<--5 川长思鸟来/ 2023年06月25日 06:01/ 0 赞/ 3 阅读
相关 单链表反转 class Node{ int value; Node next; public Node(int value){ 淩亂°似流年/ 2023年06月24日 14:25/ 0 赞/ 3 阅读
相关 反转单链表 [https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&rp=1&ru=%2Ft 本是古典 何须时尚/ 2022年11月30日 01:46/ 0 赞/ 309 阅读
相关 单链表反转 include<iostream> using namespace std; typedef struct node { i 刺骨的言语ヽ痛彻心扉/ 2022年09月18日 04:47/ 0 赞/ 279 阅读
相关 单链表反转 public Node reverse(){ Node pReverseHead=null; Node pNode=head;//当前结点开始为 痛定思痛。/ 2022年08月10日 13:46/ 0 赞/ 307 阅读
相关 单链表反转 把每次翻转看成左右两个部分的翻转,比如第一次翻转时把左边的第一个节点看成左部分,把右边的第二个节点看成右部分,来进行翻转,第二次翻转时把左边的两个节点看成一个左部分,右边的第三 本是古典 何须时尚/ 2022年08月09日 01:55/ 0 赞/ 290 阅读
相关 反转单链表 前插 class ListNode { int val; ListNode next; ListNode(i Bertha 。/ 2022年08月04日 04:18/ 0 赞/ 307 阅读
相关 反转单链表 一、反转单链表 //反转单链表链表 class ListNode { int val; ListNode next = n Bertha 。/ 2022年05月26日 06:11/ 0 赞/ 281 阅读
相关 反转单链表 ![这里写图片描述][70] //解法一:反转链表 public ListNode reverseList(ListNode head){ 以你之姓@/ 2022年05月23日 04:36/ 0 赞/ 325 阅读
相关 单链表反转 单链表的翻转是一道很基本的算法题。 方法1:将单链表储存为数组,然后按照数组的索引逆序进行反转。 方法2:使用三个指针遍历单 悠悠/ 2022年03月09日 11:26/ 0 赞/ 364 阅读
还没有评论,来说两句吧...