反转单链表 以你之姓@ 2022-05-23 04:36 324阅读 0赞 ![这里写图片描述][70] //解法一:反转链表 public ListNode reverseList(ListNode head){ ListNode root=head; ListNode pre=null; //当前节点的前面一个节点 ListNode next=null; //当前节点的后面一个节点 while (root.next!=null){ next=root.next; root.next=pre; pre=root; root=next; } root.next=pre; return root; } //解法二:反转链表 public ListNode reverseList2(ListNode head){ LinkedList<ListNode>link=new LinkedList<>(); while(head!=null){ link.add(0,head); head=head.next; } return link.get(0); } //解法三: 反转链表(单指针法) public ListNode reverseList3(ListNode head){ if(head==null||head.next==null) return head; ListNode pre=null; while(head!=null) { ListNode tmp=head.next; head.next=pre; pre=head; head=tmp; } return pre; } //遍历链表 public void printList(ListNode head) { while(head!=null) { System.out.print(head.val+" "); head=head.next; } System.out.println(); } public static void main(String[]args){ // System.out.println("Hello World!"); ListNode head=new ListNode(1); head.next=new ListNode(2); head.next.next=new ListNode(3); Solution s=new Solution(); s.printList(s.reverseList2(head)); } [70]: /images/20220523/3de12d63437f4358b72d004afd793b94.png
相关 单链表反转 常常面试我们会遇到考察列表反转的问题 如 1-->2-->3-->4-->5-->null这样的一个链表进行反转成为 null<--1<--2<--3<--4<--5 川长思鸟来/ 2023年06月25日 06:01/ 0 赞/ 2 阅读
相关 单链表反转 class Node{ int value; Node next; public Node(int value){ 淩亂°似流年/ 2023年06月24日 14:25/ 0 赞/ 2 阅读
相关 反转单链表 [https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&rp=1&ru=%2Ft 本是古典 何须时尚/ 2022年11月30日 01:46/ 0 赞/ 308 阅读
相关 单链表反转 include<iostream> using namespace std; typedef struct node { i 刺骨的言语ヽ痛彻心扉/ 2022年09月18日 04:47/ 0 赞/ 278 阅读
相关 单链表反转 public Node reverse(){ Node pReverseHead=null; Node pNode=head;//当前结点开始为 痛定思痛。/ 2022年08月10日 13:46/ 0 赞/ 305 阅读
相关 单链表反转 把每次翻转看成左右两个部分的翻转,比如第一次翻转时把左边的第一个节点看成左部分,把右边的第二个节点看成右部分,来进行翻转,第二次翻转时把左边的两个节点看成一个左部分,右边的第三 本是古典 何须时尚/ 2022年08月09日 01:55/ 0 赞/ 290 阅读
相关 反转单链表 前插 class ListNode { int val; ListNode next; ListNode(i Bertha 。/ 2022年08月04日 04:18/ 0 赞/ 306 阅读
相关 反转单链表 一、反转单链表 //反转单链表链表 class ListNode { int val; ListNode next = n Bertha 。/ 2022年05月26日 06:11/ 0 赞/ 280 阅读
相关 反转单链表 ![这里写图片描述][70] //解法一:反转链表 public ListNode reverseList(ListNode head){ 以你之姓@/ 2022年05月23日 04:36/ 0 赞/ 325 阅读
相关 单链表反转 单链表的翻转是一道很基本的算法题。 方法1:将单链表储存为数组,然后按照数组的索引逆序进行反转。 方法2:使用三个指针遍历单 悠悠/ 2022年03月09日 11:26/ 0 赞/ 364 阅读
还没有评论,来说两句吧...