博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[原创] 算法之递归(3)- 链表操作
阅读量:4624 次
发布时间:2019-06-09

本文共 4198 字,大约阅读时间需要 13 分钟。

算法之递归(3)- 链表操作

递归(2)尝试了一个单链表的遍历,同时又分析了如何添加自己的操作,是在递归调用之前,还是在递归调用之后。

今天,打算将问题深入一下,即添加相应的操作在递归的过程中。

(免责声明:下面的解法纯属娱乐 ,另外,示例代码未经编译和调试,许多想法未经实践验证。)

查找链表当中倒数第N个节点。

解法一

逐层递归,遍历到最后一个节点,并从返回的节点一次向后递归,遍历N次,找到倒数第N个节点。

private LNode targetNode = null;        private LNode FindLastNthNode(LNode head, int index)        {            if (head.Next == null)            {                return head;            }            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;                return targetNode;            }            return targetNode;        }

 

分析

1. 额外的全局性的辅助变量。

2. 时间复杂度为O(index * n),n为链表的长度。

3. 性能开销较大。

解法二(解法一的变形)

每当遍历到当前节点,即再循环向后遍历n个,如果节点遍历到最后,并且index自减等于0,说明当前节点即为要找的倒数第n个。也就是说解法一是从后向前找,而解法二是从前向后找。

private LNode targetNode2 = null;        private LNode FindLastNthNode2(LNode head, int index)        {            if (head.Next == null)                return head;            LNode tmpNode = head;            while (head != null && index >= 0)            {                head = head.Next;                index--;            }            if (head == null && index == 0)            {                targetNode2 = tmpNode;                return targetNode2;            }            return targetNode2;        }

 

分析:与解法一一样。

解法三

定义一个全局变量,用来计数,当递归从最后一个节点返回时,计数器减减,当等于0时,这个节点即是要找的倒数第N个节点。

private int counter = 0;        private LNode targetNode2;        private LNode FindLastNthNode2(LNode head, int index)        {            if (head.Next == null)            {                counter = index;                return head;            }            FindLastNthNode2(head.Next, index);            counter--;            if (counter == 0)            {                targetNode2 = head;                return targetNode2;            }            return targetNode2;        }

 

分析

1. 两个辅助变量。

2. 时间复杂度为O(n)。

3. 多余的index,累赘的counter。

 

======= 后记 =======

其实以上几个解决方案个人觉得都不够perfect。

像类似于链表这样的操作,基本就是两指针。

于是我重新给了一个方案如下。

(该段代码已经编译、运行及测试通过)

 

解法四:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication17{    class Program    {        static void Main(string[] args)        {            Node head = new Node()            {                Data = "Head"            };            Node lucas = new Node()            {                Data = "Lucas"            };            Node bill = new Node()            {                Data = "Bill"            };            Node steve = new Node()            {                Data = "Steve"            };            Node anders = new Node()            {                Data = "Anders"            };            Node jordan = new Node()            {                Data = "Jordan"            };            head.Next = lucas;            lucas.Next = bill;            bill.Next = steve;            steve.Next = anders;            anders.Next = jordan;            Program p = new Program();            Node resultNode = p.FindLastNthNode(head, 2);            Console.WriteLine(resultNode.Data);            Console.ReadLine();        }        private Node FindLastNthNode(Node node, int n)        {            if(node == null)            {                return node;            }            if(n <= 0)            {                throw new ArgumentException("n");            }            Node node1 = node;            Node node2 = node;            return this.InternalFindLastNthNode(node1, node2, n);        }        private Node InternalFindLastNthNode(Node node1, Node node2, int n)        {            if(node1 == null)            {                return node2;            }            if(n == 0)            {                return this.InternalFindLastNthNode(node1.Next, node2.Next, 0);            }            return this.InternalFindLastNthNode(node1.Next, node2, --n);        }    }    public class Node    {        public string Data { get; set; }        public Node Next { get; set; }    }}

 

 

Best Regards,

Lucas Luo

转载于:https://www.cnblogs.com/devtesters/p/4274473.html

你可能感兴趣的文章
Lua string文件类型判断和内容解析
查看>>
MyBatis的动态SQL详解
查看>>
android - 多屏幕适配相关
查看>>
Fedora Linux 18 延期至年底
查看>>
Spring Framework 3.2 RC1 发布
查看>>
基于ios开发点餐系统应用(附带源码)
查看>>
Xenia and Weights(深度优先搜索)
查看>>
文件包含漏洞进阶篇
查看>>
JavaScript的self和this使用小结
查看>>
CSS3.0:透明度 Opacity
查看>>
Arduino Wire.h(IIC/ I2C)语法
查看>>
web高并发的解决方案
查看>>
OC中的NSNumber、NSArray、NSString的常用方法
查看>>
android 用ImageSwitcher+Gallery实现图片浏览效果 分类: ...
查看>>
STM32里面的一些小函数——assert_param,PUTCHAR_PROTOTYPE
查看>>
Java分布式锁的三种实现方案(redis)
查看>>
运行客户端程序报读取配置文件出错的解决方案
查看>>
day 5 - 2 字典(dict)练习
查看>>
微引擎的自定义菜单40063错误解决
查看>>
JAVA wait(), notify(),sleep具体解释
查看>>