Question: Enter the head node of a linked list, reverse the linked list, and return the head node of the linked list. The linked list node is defined as follows:
struct ListNode{ int m_nKey; ListNode* m_pNext;};
Analysis: This is a widely used Microsoft interview question. Since this question can reflect the strict thinking of programmers, many companies have adopted this question during interviews since Microsoft.
To correctly reverse a linked list, you need to adjust the pointer direction. Code related to pointer operations is always error-prone, so it is best to perform a comprehensive analysis before you write a program. During the interview, the interviewer will be very impressed by the careful analysis and design at the beginning, because in actual software development, the design time is always longer than the code writing time. Rather than writing a piece of code that is full of loopholes, it is far better to write a piece of robust code with a lot of time.
The first method that is most likely to come up with is to re-create a single-chain table newList. Each time the first node in the list is placed behind newList. The comments are more detailed, so I won't go into details. Let's look at the Code directly:
Returns list reversesingly1_list (shortlist list) {shortlist newList; // The head node LNode * tmp of the new linked list; // points to the first node of the list, that is, if the node to be removed is NULL or the memory allocation fails, NULL // if (list = NULL | (newList = (partition list) is returned) malloc (sizeof (LNode) = NULL) {return NULL;} // initialize newList // newList-> data = list-> data; newList-> next = NULL; // place the first node of the list in sequence to the first node position of newList. // while (list-> next! = NULL) {tmp = newList-> next; // Save the subsequent node newList in newList-> next = list-> next; // place the first node of the list in newList. list-> next = list-> next; // remove this node from the list newList-> next = tmp; // restore the pointer to the subsequent node in newList} /// the original header node should be released, and returns the pointer to the new header node // free (list); return newList ;}
The second method is to place the node after the original first node after the list every time, which is the original single-chain table.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1925095449-0.png "title =" QQ20131006144637.png "alt =" 14347716.png"/>
To reverse this single-chain table, we first point the next field of the header node to node 2, then the next field of Node 1 to node 3, and finally point the next field of Node 2 to node 1, after the first exchange is completed, the order is changed to Header-node 2-node 1-node 3-node 4-NULL. Then, in the same exchange, node 3 is moved to the front of Node 2, then, move node 4 to the front of Node 3 to complete the reversal. If you have the idea, you should write the code:
LinkedList ReverseSinglyLinkedList(LinkedList list){ LNode *tmp = NULL; LNode *p = NULL; if (list == NULL) { return NULL; } tmp = list->next; while (tmp->next != NULL) { p = tmp->next; tmp->next = p->next; p->next = list->next; list->next = p; } return list;}
The third method is similar to the second method. The second method is to move the following node forward to the end of the first node, the third method is to move the previous node to the end of the original last node. The idea is similar to that of the second method, so no code is pasted.
This article from the "Yi fall Dusk" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/1305094