The sword refers to the Offer - print the linked list from end to end

Topic description: Enter the head node of a linked list, and print out the value of each node from the end to the beginning.

Analysis: Print from the end to the beginning, that is, first in, last out. According to this, you can think about whether it can be implemented with a stack. This is possible. The values ​​of the nodes are pushed into the stack in order, and all the values ​​are pushed into the stack and then popped out of the stack until the stack is empty. Is there any way other than using the stack? Yes, there is another one similar to stack, that is recursion. Recursion is essentially a stack structure, so there are two solutions.

code show as below:

Declare the node object, and provide the necessary methods:

class ListNode
    {
        public object m_nValue;
        public ListNode m_pNext;
        public ListNode(object data=null)
        {
            m_nValue = data;
            m_pNext = null;
        }
        //Associate two nodes
        internal static void ConnectListNodes(ListNode currentNode,ListNode nextNode)
        {
            currentNode.m_pNext = nextNode;
        }
        // delete the entire linked list
        internal static void DestroyListNodes(ListNode pHead)
        {
            ListNode currentNode = pHead;
            ListNode nextNode = null;
            while(currentNode!=null)
            {
                nextNode = currentNode.m_pNext;
                //Execute delete (no corresponding method found)
                currentNode = nextNode;
            }
        }
        
    }

The solution to store the value on the stack:

private static void PrintListReversingly_Iteratively(ListNode pHead)
        {
            Stack<object> nodes = new Stack<object>();
            ListNode pNode = pHead;
            // push the stack
            while(pNode!=null)
            {
                nodes.Push(pNode.m_nValue);
                pNode = pNode.m_pNext;
            }
            // print and pop
            while(nodes.Count>0)
            {
                object value = nodes.Peek();
                Console.Write("{0} ", value);
                nodes.Pop();
            }
        }

Recursive solution:

private static void PrintListReversingly_Recursively(ListNode pHead)
        {
            ListNode pNode = pHead;
            if(pNode.m_pNext!=null)
            {
                PrintListReversingly_Recursively(pNode.m_pNext);
            }
            Console.Write("{0} ", pNode.m_nValue);
        }

testing method:

static void Test(ListNode pHead)
        {
            PrintListReversingly_Iteratively(pHead);
            Console.WriteLine();
            PrintListReversingly_Recursively(pHead);
            Console.WriteLine();
        }

        // 1->2->3->4->5
        static void Test1()
        {
            ListNode pNode1 = new ListNode(1);
            ListNode pNode2 = new ListNode(2);
            ListNode pNode3 = new ListNode(3);
            ListNode pNode4 = new ListNode(4);
            ListNode pNode5 = new ListNode(5);

            ListNode.ConnectListNodes(pNode1, pNode2);
            ListNode.ConnectListNodes(pNode2, pNode3);
            ListNode.ConnectListNodes(pNode3, pNode4);
            ListNode.ConnectListNodes(pNode4, pNode5);

            Test(pNode1);
        }

        // linked list with only one node
        static void Test2()
        {
            ListNode pNode1 = new ListNode(1);

            Test(pNode1);
        }

        //empty list
        static void Test3()
        {
            ListNode pNode1 = new ListNode();

            Test(pNode1);
        }

Main method:

static void Main(string[] args)
        {
            Test1();
            Test2 ();
            Test3();
            Console.ReadKey();
        }
Summary: The main point is to think about the stack, and be careful when using recursive methods. When the linked list is very long, it will lead to a deep level of function calls, which may cause the function call stack to overflow.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326661804&siteId=291194637