Reverse linked list

Source: Internet
Author: User

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. In order to clearly analyze the complex process of adjusting pointers, we can use graphs for visual analysis. Assume that l, m, and n are three adjacent nodes:? B ?...? L mà n à... After several operations, we have adjusted the pointer before node l. The m_pNext pointer of these nodes points to the previous node. Now we traverse to the node m. Of course, we need to adjust the m_pNext pointer of the node to point it to node l. Note that the linked list is disconnected once the pointer pointing is adjusted, as shown in:? B ?... L? Mn... Because no Pointer Points to node n, we cannot traverse node n any more. Therefore, to avoid the disconnection of the linked list, we need to save n before adjusting m's m_pNext. Next, we try to find the head node of the reverse linked list. It is not difficult to find that the head node of the inverted linked list is the tail node of the original linked list. What node is the end node? It is the node where m_pNext is a null pointer. [Java] public class Test_19 {public static void main (String [] args) {int [] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9 }; signalList list = new signalList (); for (int I = 0; I <arr. length; I ++) {list. add (arr [I]);} list. printlist (list. getHead (); list. printlist (reverseList (list. getHead ();} public static ListNode reverseList (ListNode head) {if (head = null) {return null;} ListNode reverseHead = null; // List of new head nodes after the chain table is reversed Node pnode = head; ListNode ppre = null; while (pnode! = Null) {ListNode pnext = pnode. getNext (); if (pnext = null) {reverseHead = pnode;} pnode. setNext (ppre); ppre = pnode; pnode = pnext;} return reverseHead;} class ListNode {private int key; private ListNode next; public int getKey () {return key ;} public void setKey (int key) {this. key = key;} public ListNode getNext () {return next;} public void setNext (ListNode next) {this. next = next ;} Public ListNode (int key, ListNode next) {this. key = key; this. next = next ;}} class signalList {private ListNode head; public ListNode getHead () {return head;} public void setHead (ListNode head) {this. head = head;} private ListNode tail; public ListNode getTail () {return tail;} public void setTail (ListNode tail) {this. tail = tail;} private int size; public void add (int I) {ListNode newnode = New ListNode (I, null); if (head = null) {head = newnode; tail = newnode; size ++;} else {tail. setNext (newnode); tail = newnode; size ++ ;}} www.2cto.com public void printlist (ListNode head) {if (head = null) {return;} ListNode pnode = head; while (pnode! = Null) {System. out. println (pnode. getKey (); pnode = pnode. getNext ();}}}

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.