单链表翻转 非递归方法 返回数组

单链表翻转 非递归方法
返回数组


package com.link;


import java.util.ArrayList;
import java.util.List;

public class Solution {
  
    List<Integer> list =new ArrayList<>();
    public List<Integer> reverseStore(ListNode head) {
        // write your code here
        ListNode pre=null;
        ListNode next=null;
        ListNode cur=head;

        while(cur!=null){
        	System.out.println(cur.val);
                next=cur.next;
                cur.next=pre;
                pre=cur;
                cur=next;
                
                
        }
        cur=pre;//因为最后next指向为空 所以 需要使用pre
        while(cur!=null){
            list.add(cur.val);
            cur=cur.next;
        }
        
        return list;
    }
    public static void main(String[] arg) {
    	
    	ListNode node1=new ListNode(1);
    	ListNode node2=new ListNode(2);
    	
    	ListNode node3=new ListNode(3);
    	
    	ListNode node4=new ListNode(4);
    	
    	
    	node1.next=node2;
    	node2.next=node3;
    	node3.next=node4;
    	Solution solution=new Solution();
    	List list=solution.reverseStore(node1);
    	
    	for (int i=0;i<list.size();i++) {
    		System.out.println(list.get(i));
    	}
    
    	
    	
    	
    	
    }
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值