Title: Define a function, enter the head node of a linked list, reverse the list and output the head node of the inverted list.
There is always a lot of pointer action to solve the problem associated with the list, and the code of the pointer operation is always error-prone. A lot of interviewers like to be linked to the list of problems, is to use the pointer to work to examine the candidate's coding skills. In order to avoid mistakes, we'd better conduct a thorough analysis first. In the actual software development cycle, the design time is usually no shorter than the encoding time. In the interview we do not rush to write code, but at the beginning of careful analysis and involved, this will give the interviewer a good impression. Rather than giving out a piece of flawed code, it's better to carefully analyze and write robust code.
In order to correctly reverse a linked list, you need to adjust the direction of the pointer in the linked list. In order to analyze the complex process of adjusting pointers, we can use graphs to analyze them visually. In the linked list shown in the figure, H,i,j is a node adjacent to 3 elder brothers. Assuming that after a number of operations, we have adjusted the pointer before the node H, and the m_pnext of these nodes point to a previous node. Next we point the m_pnext of I to H, which is shown in the list structure B.
It is not difficult to notice that because the m_pnext of node I points to its previous node, we cannot traverse the node J in the linked list. To prevent the list from disconnecting at node I, we need to save the node J before adjusting the node I m_pnext.
In other words, when we adjust the M_pnext pointer of node I, I need to know the node I in addition to the first node H, because we need to point the node I m_pnext to Node H, and we also implement the need to save I node J to prevent the list from disconnecting. Accordingly, we need three pointers, pointing to the node that is currently traversed, its previous node, and the latter node.
Finally, we try to find the head node of the inverted list. It is not difficult to analyze the inverted list of the head node is the end of the original linked list nodes. What knot is the tail node, which is naturally a m_pnext null junction.
The analysis writes out the following code:
/** * Defines a function that enters the head node of a linked list, reverses the list and outputs the inverted head node. */package Swordforoffer;import Utils. listnode;/** * @author Jinshuangqi * * August 1, 2015 */public class E16reverselist {public ListNode reverselist (ListNode head) {if (head = = NULL) return null; ListNode prelistnode = null; ListNode Nowlistnode = Head;while (Nowlistnode.next! = null) {ListNode nextlistnode = Nowlistnode.next; Save the next node Nowlistnode.next = Prelistnode; The current node points to the previous node Prelistnode = Nowlistnode; Predecessor nodes to the incumbent node Nowlistnode = nextlistnode;//current node to the next junction}return Nowlistnode;} public static void Main (string[] args) {ListNode head = new ListNode (); ListNode second = new ListNode (); ListNode third = new ListNode (); ListNode forth = new ListNode (); head.next = Second;second.next = Third;third.next = Forth;head.data = 1;second.data = 2;th Ird.data = 3;forth.data = 4; E16reverselist test = new E16reverselist (); ListNode result = Test. Reverselist (head); System.out.println (Result.data);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sword Point of Offer question (Java Edition): Reverse linked list