输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。
如输入{1,2,3}的链表如下图:
返回一个数组为[3,2,1]
0 <= 链表长度 <= 10000
import java.util.*; /** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */ import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> initLinkedList = new ArrayList<>(); ListNode temptNode = listNode; while (temptNode != null) { initLinkedList.add(temptNode.val); temptNode = temptNode.next; } temptNode = listNode; ArrayList<Integer> reversedLinkedList = new ArrayList<>(); ListNode reversedListNode = reversedLinkedList(listNode); while (reversedListNode != null) { reversedLinkedList.add(reversedListNode.val); reversedListNode = reversedListNode.next; } return reversedLinkedList; } public static ListNode reversedLinkedList(ListNode listNode) { if (listNode == null || listNode.next == null) { return listNode; } else { ListNode temptNode = listNode; ListNode priorNode = null; ListNode nextNode = listNode.next; while (nextNode.next != null) { priorNode = temptNode; temptNode = nextNode; nextNode = nextNode.next; temptNode.next = priorNode; } nextNode.next = temptNode; listNode.next = null; listNode = nextNode; return listNode; } } }
import java.util.*; /** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */ import java.util.ArrayList; public class Solution { public ArrayList printListFromTailToHead(ListNode listNode) { if(listNode == null){ return new ArrayList(); } ListNode next = null; ListNode prev = null; while(listNode != null){ next = listNode.next; listNode.next = prev; prev = listNode; listNode =next; } listNode = prev; ArrayList reslut = new ArrayList(); while(listNode != null){ reslut.add(listNode.val); listNode = listNode.next; } return reslut; } }
import java.util.*; /** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */ import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> resultList = new ArrayList<>(); while(listNode != null){ resultList.add(listNode.val); listNode = listNode.next; } ArrayList<Integer> midList = new ArrayList<>(); for(int i = resultList.size()-1; i >= 0;i--){ midList.add(resultList.get(i)); } return midList; } }
最优解:链表利用数组原地翻转 import java.util.*; /** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */ import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { //原地调换位置(链表反转) if(listNode == null){ return new ArrayList<Integer>(0); } int count = 0; ListNode tmp = listNode; while(tmp != null){ count++; tmp = tmp.next; } int[] res = new int[count]; int k = count - 1; while(listNode != null){ res[k--] = listNode.val; listNode = listNode.next; } ArrayList<Integer> arr = new ArrayList<Integer>(); for(int i = 0;i < res.length;i++){ arr.add(res[i]); } return arr; } }
import java.util.*; /** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */ import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { // 方法四:利用头插法 ListNode head=new ListNode(-1) ; while(listNode!= null){ //把新加入的节点当做node ListNode temp=listNode.next; //需要先把下一个节点的值存储起来,不然就会失效 listNode.next=head.next; head.next=listNode; listNode=temp; } ArrayList <Integer> res = new ArrayList<Integer>(); head=head.next; while(head!=null){ //访问这个新链表的值即可 res.add(head.val); head=head.next; } return res; /* // 方法三:利用递归 访问改节点则需访问该节点的子节点 ArrayList <Integer> res=new ArrayList<Integer>();//定义在方法外边 if (listNode != null) { printListFromTailToHead(listNode.next); res.add(listNode.val); } return res; // 方法二:利用Aarraylist.add( index,value)方法来实现 ArrayList <Integer> res=new ArrayList<Integer>(); while (listNode!=null){ res.add(0,listNode.val); listNode=listNode.next; } return res; //方法一:利用栈,从头到尾依次入栈,然后依次出栈 ArrayList<Integer> res=new ArrayList<Integer>(); Stack<ListNode> stack =new Stack<ListNode>(); while(listNode != null){ stack.add(listNode); listNode=listNode.next; } while(stack.size()>0){ res.add(stack.pop().val); } return res; */ } }
public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> res = new ArrayList<Integer>(); while(listNode != null){ res.add(0,listNode.val); listNode = listNode.next; } return res; } }
import java.util.*; /** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */ import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> list = new ArrayList(); while(listNode != null){ list.add(0,listNode.val); listNode = listNode.next; } return list; } }
public class Solution { private ArrayList<Integer> list = new ArrayList<>(); public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { reserve(listNode); return list; } private void reserve(ListNode listNode) { if (listNode == null) { return; } reserve(listNode.next); list.add(listNode.val); } }
import java.util.*; import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { //多使用一个栈的数据结构,使节点值逆序 //时间复杂度为O(N),空间复杂度O(N) Stack<Integer> s=new Stack<Integer>(); ArrayList<Integer> res=new ArrayList<>(); while(listNode!=null){ s.push(listNode.val); listNode=listNode.next; } while(!s.isEmpty()){ res.add(s.pop()); } return res; } }
import java.util.Collections; import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> ans = new ArrayList<>(); printListFromTailToHeadCore(ans,listNode); return ans; } private void printListFromTailToHeadCore(ArrayList<Integer> ans, ListNode listNode) { if (listNode == null){ return; } printListFromTailToHeadCore(ans,listNode.next); ans.add(listNode.val); } }
import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> listres= new ArrayList(); while(listNode != null){ int i = 0; listres.add(i,listNode.val); i++; listNode = listNode.next; } return listres; } }