版权声明:本文为博主原创文章,转载请注明出处:https://twocups.cn/index.php/2020/01/16/13/
最近我遇到“链表中倒数第k个结点”这道算法题,做完后发现大家用的都是双指针解法,没有人用递归解法做。不过递归用到了栈,所以空间复杂度就不是 O(1) 了。但这里不讨论算法的优劣,我只是觉得用递归思想解这题很有意思,所以和大家分享一下递归解法。
题目描述
输入一个链表,输出该链表中倒数第k个结点。
链表结构
public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }
双指针解法
public ListNode FindKthToTail(ListNode head,int k) { ListNode first,last; first = last = head; int i = 0; for(;last != null;i++){ if(i>=k) first = first.next; last = last.next; } return i < k ? null :first; }
递归解法
int count = 1; ListNode node; public ListNode FindKthToTail(ListNode head,int k) { if(head != null){ this.FindKthToTail(head.next,k); if(count++ == k){ node = head; } } return node; }
#HeLLo#
I think that what you said made a lot of sense. But, think on this, what if you typed a catchier title? I ain’t saying your content is not solid., but suppose you added a title that makes people want more? I mean %BLOG_TITLE% is a little plain. You might glance at Yahoo’s front page and note how they create post headlines to get people to open the links. You might add a video or a related pic or two to grab readers interested about what you’ve got to say. Just my opinion, it might bring your posts a little livelier.
WhatsApp sanal telefon numaras?
I believe everything said made a lot of sense. But, think about this, suppose you were to create a killer headline? I ain’t saying your content isn’t solid., however suppose you added something that makes people desire more? I mean %BLOG_TITLE% is kinda boring. You should peek at Yahoo’s front page and watch how they create article headlines to get people to click. You might try adding a video or a picture or two to get readers excited about what you’ve got to say. Just my opinion, it might make your website a little bit more interesting.
https://institute.com.ua/elektroshokery-yak-vybraty-naykrashchyy-variant-dlya-samooborony-u-2025-roci
This info is priceless. How can I find out more?
hafilat darb ae
WOW just what I was searching for. Came here by searching for %keyword%
hafilat
Hey I am so happy I found your blog page, I really found you by accident, while I was looking on Digg for something else, Anyhow I am here now and would just like to say kudos for a tremendous post and a all round thrilling blog (I also love the theme/design), I don’t have time to browse it all at the minute but I have saved it and also added your RSS feeds, so when I have time I will be back to read more, Please do keep up the superb work.
https://www.fksmederevo.com/choosing-the-best-version-of-plinko-gambling.html
Hi, the whole thing is going sound here and ofcourse every one is sharing facts, that’s truly excellent, keep up writing.
https://endiny.shop/plinko-games-free-unblocked-versions-for-all-ages.html