Get middle element of a linked-list
来源:互联网 发布:戴面具的网络歌手 编辑:程序博客网 时间:2024/06/05 20:06
Suppose there're odd number of elements in a linked-list, how to get the middle element in it? I find a clever way to achieve this on internet. Two pointers point to the linked-list. One is slow pointer and the other is quick pointer. Each time slow pointer moves one step, while the quick pointer moves two steps. When the quick pointer reaches the end of the list, the slow pointer is pointing to the middle of the list.
The slow and quick pointers solution can also be used to check whether there's a loop in linked list in O(n) time. If quick pointer reaches slow pointer somewhere sometime, it means there's a loop in linked list.
Refering to the mid function followed. It is a solution written in Haskell.
- Get middle element of a linked-list
- 1007. Find the middle element in a Linked List
- PIQ18: Delete middle node of a linked list
- 【11】Delete a node in the middle of a single linked list
- [CrackCode] 2.3 Delete a node in the middle of a single linked list
- Q.2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only acce
- Find the middle point of the linked list
- LinkedList---Delete Node in the Middle of Singly Linked List
- #372 Delete Node in the Middle of Singly Linked List
- Delete Node in the Middle of Singly Linked List
- 【LeetCode】Middle of Linked List 查找链表中点
- Find the nth to last element of a singly linked list
- 【10】Find nth to last element of a singly linked list
- [CrackCode] 2.2 Find the nth to last element of a singly linked list
- Q2.2 Find the kth to last element of a singly linked list
- To find the kth to Last Element of a Singly Linked List
- Reverse Second Half of a Linked List
- LintCode 372: Delete Node in the Middle of Singly Linked List
- x-b
- b-x
- JVM的垃圾回收机制详解和调优
- 解决出现'findstr' 不是内部或外部命令,jboss
- as3与c++交互
- Get middle element of a linked-list
- com.microsoft.sqlserver.jdbc.SQLServerException: 系统内存不足。请对大型 ResultSet 使用服务器端游标: Java heap space。ResultSet 大小:417,666,054。JVM 总内
- c++跨平台
- C语言实现简单单链表
- 回调函数用法
- CSDN应该拥抱iOS+OSX
- Java类加载原理解析
- MFC对话框程序启动的时候直接隐藏、在任务栏不显示图标
- MVC中几个关于布局的小笔记