topic:
Reverse a singly linked list.
Example:
Input: 1-> 2-> 3-> 4- > 5-> NULL Output: 5-> 4-> 3-> 2- > 1-> NULL
Advanced:
You can reverse iterative or recursively list. Can you solve this question in two ways?
answer:
First on the code:
/ ** * Definition List for Singly-linked. * Public class ListNode { * int Val; * ListNode Next; * ListNode (int X) {X = Val;} *} * / class Solution { public ListNode reverseList (ListNode head) { IF (head == null || head.next == null ) // list is empty or only one element { return head; } the else { ListNode newHead = reverseList (head.next); // recursive, by the statement after that, the head node after node has completed all the flip, this time, just need to head node on the new list is like the last = head head.next.next; // head node should be placed head.next.next head.next = null ; // head is the last node, the null = head.next return newHead; // return a new head node, as the starting point recursive } } }
Recursive list above inverted code
Recursive termination condition: . 1, head is empty. 2, head.next is empty
Recursive process: we make a new head node newHead, the new head node newHead head.next should be finished after the head flip. At the same time head should be placed behind head.next and make head.next = null. So we put a new face on the last head of the list (recursively until the last plane to achieve this step, let's draw a diagram shows what this recursive process).
Analysis of Algorithms:
Involves recursive, recursive every time the time complexity is O (1), so the whole time complexity is recursive O (n), n is the chain length: Analysis time complexity, the spatial complexity. For space complexity, recursive algorithm does not involve opening up new space, so the space complexity is O (n), (where the spatial complexity analysis seems to have problems ......)
Is there a non-recursive algorithm which? some! In fact, most of the non-recursive recursive algorithms are possible to achieve, but a little bit complicated.
Then the first non-recursive algorithm code, and then analyze the algorithm it!
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode reverseList(ListNode head) { ListNode newNode=new ListNode(-1); ListNode node1,node2,node3; newNode.next=head; node1=newNode; if(node1.next==null||node1.next.next==null)//和上面一样的原因 { Return head; } the else { node2 = node1.next; node3 = node2.next; the while (! Node3 = null ) // The following code is the key to achieve, and each node will be transferred to a position of the head node, then node2, node3 after the pointer moves. { Node2.next = node3.next; ListNode TEMP = node1.next; node1.next = node3; node3.next = TEMP; node3 = node2.next; } } return newNode.next; } }
Analysis of Algorithms:
1, a virtual set of the newNode head node, and three pointers, node1, node2, node3.
2, so newNode.next point head.
3, if newNode.next.next or newNode.next == null is returned directly to the original list
4. Otherwise, let node2 point node1.next, node3 point node2.next
5, if node3 not empty, execute the following sections:
node3 node points to be processed, node2 point to a node to be processed before node.
So node2.next point node3.next
Set up temporary node temp, storage node1.next
Node3 so node1.next point, the node is about to be processed on the position of the head node and to make node3.next temp
Update node3 value node2.next
The results newNode.next return after flipping
Here is the code to run hand-drawn process: Flip to the abc cba
Analysis Algorithm: time complexity of O (n), n is the chain length and space complexity, once per cycle, a temporary node to open up space, only a last reserved, so the space complexity O (1).