Sword refers to the Java Implementation of offer programming questions-interview question 5 prints the linked list from start to end and offer from end to end

Source: Internet
Author: User

Sword refers to the Java Implementation of offer programming questions-interview question 5 prints the linked list from start to end and offer from end to end
Topic Description * sword refers to offer interview question 5: print the linked list from the end to the endEnter the head node of a linked list and print the value of each node from the end to the end.

Solution 1: print the nodes in the traversal table first, and use the stack to implement this sequence.
Solution 2: the essence of stack is recursion. When printing a node, print the node next to it, and then print the node itself to implement reverse printing.
Solution 3: copy the elements in the linked list to the ArrayList and print the elements in the ArrayList in reverse order. Because the ArrayList uses arrays at the underlying layer, the same principle applies to arrays.
Solution 4: the first three solutions do not modify the structure of the linked list when you print the linked list. If you are allowed to modify the structure of the linked list, you can reverse the node pointer in the linked list, change the direction of the linked list, and print the linked list with the changed direction again.

 
1 package Solution; 2 3 import java. util. arrayList; 4 import java. util. stack; 5 6/** 7 * sword refers to offer interview question 5: print the linked list from the end to the head 8 * input a head node of the linked list, print the value of each node from the end to the header 9 * solution 1: print the nodes in the traversal table first, and then use the stack to implement this order. 10 * solution 2: the essence of stack is recursion. The method of recursion is used directly. When printing a node, the node following it is printed first, and then the node itself is printed, implement reverse printing 11 * solution 3: copy the elements in the linked list to the ArrayList, and print the elements in the ArrayList in reverse order 12 * solution 4: the first three solutions do not modify the structure of the linked list when printing the linked list. 13 * if you are allowed to modify the structure of the linked list, you can reverse the node pointer in the linked list to change the direction of the linked list, then print the chain table after changing the direction again. 14 * @ author GL 15*16 */17 public class No5PrintListFromTailToHead {18 19 public static void main (String [] args) {20 ListNode node1 = new ListNode (1); 21 ListNode node2 = new Lis TNode (2); 22 ListNode node3 = new ListNode (3); 23 ListNode node4 = new ListNode (4); 24 ListNode node5 = null; 25 ListNode node6 = new ListNode (6); 26 ListNode node7 = new ListNode (); 27 node1.next = node2; 28 node2.next = node3; 29 node3.next = node4; 30 printListFromTailToHead (node1 ); 31 printListFromTailToHead (node5); 32 printListFromTailToHead (node6); 33 printListFromTailToHead (node7); 34 // print the linked list 35 prin in reverse order using the stack TListFromTailToHeadByStack (node1); 36 // print the linked list in reverse order using recursion 37 printListFromTailToHead (node1); 38 // print 39 printListFromTailToHeadByReverseList (node1) using recursive inversion ); 40 // use ArrayList to print the linked list in reverse order 41 printListFromTailToHeadByArrayList (node1); 42} 43 44/* 45 * solution 1: By using the stack structure, push the value of the first traversal node into the stack. After the traversal, print 46 */47 public static void printListFromTailToHeadByStack (ListNode node) in reverse order by the elements in the stack popped up) {48 Stack <Integer> stack = new Stac K <Integer> (); 49 while (node! = Null) {50 stack. push (node. val); 51 node = node. next; 52} 53 while (! Stack. isEmpty () {54 System. out. print (stack. pop () + ","); 55} 56} 57 58 59/* 60 * solution 2: print the linked list 61 */62 public static void printListFromTailToHead (ListNode node) in reverse order by recursion) {63 if (node! = Null) {64 if (node. next! = Null) {65 printListFromTailToHead (node. next); 66} 67 System. out. print (node. val + ","); 68} 69 else 70 System. out. println ("the input linked list is empty"); 71} 72 73/* 74 * solution 3: print the linked list in reverse order using ArrayList 75 */76 public static void printListFromTailToHeadByArrayList (ListNode node) {77 if (node = null) 78 System. out. print ("input linked list is null"); 79 ArrayList <Integer> arrayList = new ArrayList <Integer> (); 80 while (node! = Null) {81 arrayList. add (node. val); 82 node = node. next; 83} 84 for (int I = arrayList. size ()-1; I> = 0; I --) {85 System. out. print (arrayList. get (I) + ","); 86} 87} 88 89 90/* 91 * solution 4: recursive reverse linked list, print 92 */93 public static void printListFromTailToHeadByReverseList (ListNode node) {94 ListNode reversedNode = reverse (node); 95 while (reversedNode! = Null) {96 System. out. print (reversedNode. val + ","); 97 reversedNode = reversedNode. next; 98} 99 100} 101 // recursive inversion 102 private static ListNode reverse (ListNode head) {103 if (head. next = null) 104 return head; 105 ListNode reversedListNode = reverse (head. next); 106 head. next. next = head; 107 head. next = null; 108 return reversedListNode; 109} 110 111} 112 class ListNode {113 int val; 114 ListNode next = null; 115 public ListNode () {116 117} 118 public ListNode (int value) {119 this. val = value; 120} 121}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.