1 reverse list
Example. 1: LeetCode 206. This question is simple but it is one of many companies interview questions. Shown before and after inversion as follows:
In the process of inversion is mainly moved, as shown below between the pointers according to:
class Solution { public ListNode reverseList (ListNode head) { ListNode PREV = null ; the while (! head = null ) { // before each modification of a first modified head.next backup otherwise the head can not find head.next ListNode nextTemp = head.next; // 2 head.next temp for modifying the information stored the last time the head node head.next = PREV; // update the stored information of the head node TEMP. 3 PREV = head; // . 4 continued traversing head = nextTemp; } // returns the new head of the linked list node returnPREV; } } // recursive version implementation class Solution { public ListNode reverseList (ListNode head) { // recursive termination condition IF (head == null || head.next == null ) return head; // recursive process operation is started from the last element of ListNode P = reverseList (head.next); head.next.next = head; head.next = null ; return P; } }
Have a problem like this: LeetCode: 92
Note: For lists some basic questions operation: LeetCode 83,86,328,2,445
2 to set up the list of virtual head node
Example. 1: LeetCode 203. To delete a node for illustrating the following, the node 3 is set to be deleted.
In the above logic to delete the last element it is also true, as the last element next to NULL. But the problem is that the first element does not hold, because cur refers to the need to be deleted before a node node.
0