该方法在不借助计数器变量实现寻找中位数的功能。原理是:快指针的移动速度是慢指针移动速度的2倍,因此当快指针到达链表尾时,慢指针到达中点。程序还要考虑链表结点个数的奇偶数因素,当快指针移动x次后到达表尾(1+2x),说明链表有奇数个结点,直接返回慢指针指向的数据即可。如果快指针是倒数第二个结点,说明链 ...
分类:
其他好文 时间:
2017-03-14 19:28:30
阅读次数:
174
Givenalinkedlist,removethenthnodefromtheendoflistandreturnitshead.
Forexample,
Givenlinkedlist:1->2->3->4->5,andn=2.
Afterremovingthesecondnodefromtheend,thelinkedlistbecomes1->2->3->5.
Note:
Givennwillalwaysbevalid.
Trytodothisinonepa..
分类:
编程语言 时间:
2017-03-12 11:56:16
阅读次数:
146
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. 使用快慢指针, fast每次前进两 ...
分类:
其他好文 时间:
2016-12-01 14:13:41
阅读次数:
130
2.2 链表中倒数第k个结点 输入一个链表,输出该链表中倒数第k个结点。 思路:快慢指针(error: 判断是否有可行解,否则返回null, while, if 后加空格) 1 /* 2 public class ListNode { 3 int val; 4 ListNode next = nul ...
分类:
其他好文 时间:
2016-11-28 01:12:00
阅读次数:
237
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 分析: 根据一个有序链表,得到一个平衡二叉搜索树,主要是根据快慢指针得到链表的中 ...
分类:
其他好文 时间:
2016-11-12 23:04:52
阅读次数:
202
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For ...
分类:
其他好文 时间:
2016-11-10 09:53:26
阅读次数:
224
判断链表是否有环,定义指针一快(走2部)一慢(走1部),相遇即有环。 两个指针,一快一慢,有环,则相遇必在环内,找出相遇节点 接下来,就可以统计环中节点个数,找出环的入口节点 设节点个数为n,快指针先走n步,然后快慢指针一起一步一步走,相遇节点即环入口节点。 ...
分类:
其他好文 时间:
2016-10-26 19:48:05
阅读次数:
209
141.LinkedListCycleGivenalinkedlist,determineifithasacycleinit.Followup:Canyousolveitwithoutusingextraspace?题目大意:判断一个单链表是否存在环。思路:采用快慢指针来处理。代码如下:/**
*Definitionforsingly-linkedlist.
*structListNode{
*intval;
*ListNode*next..
分类:
其他好文 时间:
2016-08-13 06:37:45
阅读次数:
212
Linked List Cycle、 Linked List Cycle II、 Remove Nth Node from End of List、 Intersection of Two Linked Lists ...
分类:
其他好文 时间:
2016-07-22 08:48:10
阅读次数:
1332