[Original] algorithm recursion (3)-list operation

Source: Internet
Author: User

Recursive algorithm (3)-list operation

Recursion (2) attempts a single-linked list traversal, and also analyzes how to add its own operations, either before the recursive call or after the recursive call.

Today, I'm going to dive into the problem by adding the appropriate actions in the process of recursion.

(Disclaimer: The following solution is purely entertaining, in addition, the sample code is not compiled and debugged, many ideas are not verified by practice.) )

Find the penultimate node of the list.

Solution One

Recursively recursive, traverse to the last node, and recursively back from the returned node, traverse n times, and find the nth node.

        PrivateLnode TargetNode =NULL; PrivateLnode Findlastnthnode (Lnode head,intindex) {            if(head. Next = =NULL)            {                returnHead; } findlastnthnode (head.            Next, index); Lnode Tmpnode=Head;  while(head. Next! =NULL) && (Index >0) ) {head=head.                Next; Index--; }            if(head. Next = =NULL&& index = =0) {TargetNode=Tmpnode; returnTargetNode; }            returnTargetNode; }

Analysis

1. An additional global helper variable.

2. The time complexity is O (index * n), and n is the length of the linked list.

3. Higher performance overhead.

Solution two (deformation of solution one)

Whenever traversing to the current node, that is, looping backwards through n, if the node is traversed to the last, and index decrement equals 0, the current node is the penultimate nth to find. That is to say, the solution is to look forward from the back, and the second is to look backwards.

        PrivateLnode TargetNode2 =NULL; PrivateLnode FindLastNthNode2 (Lnode head,intindex) {            if(head. Next = =NULL)                returnHead; Lnode Tmpnode=Head;  while(Head! =NULL&& Index >=0) {Head=head.                Next; Index--; }            if(Head = =NULL&& index = =0) {TargetNode2=Tmpnode; returnTargetNode2; }            returnTargetNode2; }

Analysis: Same as solution one.

Solution Three

Defines a global variable that is used to count when a recursive return from the last node is reduced, and when it equals 0 o'clock, the node is the nth node to find.

        Private intCounter =0; PrivateLnode TargetNode2; PrivateLnode FindLastNthNode2 (Lnode head,intindex) {            if(head. Next = =NULL) {counter=index; returnHead; } FindLastNthNode2 (head.            Next, index); Counter--; if(Counter = =0) {TargetNode2=Head; returnTargetNode2; }            returnTargetNode2; }

Analysis

1. Two auxiliary variables.

2. The time complexity is O (n).

3. Redundant index, cumbersome counter.

======= PostScript =======

In fact, the above several solutions personally feel not enough perfect.

An operation like a linked list is basically two pointers.

So I gave a solution to the following.

(The code has been compiled, run, and tested)

Solution Four:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceconsoleapplication17{classProgram {Static voidMain (string[] args) {Node Head=NewNode () {Data="Head"            }; Node Lucas=NewNode () {Data="Lucas"            }; Node Bill=NewNode () {Data="Bill"            }; Node Steve=NewNode () {Data="Steve"            }; Node Anders=NewNode () {Data="Anders"            }; Node Jordan=NewNode () {Data="Jordan"            }; Head. Next=Lucas; Lucas. Next=Bill; Bill. Next=Steve; Steve. Next=Anders; Anders. Next=Jordan; Program P=NewProgram (); Node ResultNode= P.findlastnthnode (Head,2);            Console.WriteLine (Resultnode.data);        Console.ReadLine (); }        PrivateNode Findlastnthnode (node node,intN) {if(node = =NULL)            {                returnnode; }            if(N <=0)            {                Throw NewArgumentException ("N"); } Node Node1=node; Node Node2=node; return  This.        Internalfindlastnthnode (Node1, Node2, N); }        PrivateNode Internalfindlastnthnode (node Node1, node Node2,intN) {if(Node1 = =NULL)            {                returnNode2; }            if(n = =0)            {                return  This. Internalfindlastnthnode (Node1. Next, Node2. Next,0); }            return  This. Internalfindlastnthnode (Node1. Next, Node2,--N); }    }     Public classNode { Public stringData {Get;Set; }  PublicNode Next {Get;Set; } }}

Best regards,

Lucas Luo

[Original] algorithm recursion (3)-list operation

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.