Sorted insert for circular linked list
来源:互联网 发布:2016开淘宝店还赚钱吗 编辑:程序博客网 时间:2024/06/08 19:14
在一个已排序的循环聊表中插入一个新节点,返回新的头节点
参考:点击打开链接
package list;import z_dataStructure.ListNode;//http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=144400&extra=page%3D9%26filter%3Dsortid%26sortid%3D311%26searchoption%5B3046%5D%5Bvalue%5D%3D2%26searchoption%5B3046%5D%5Btype%5D%3Dradio%26sortid%3D311//http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=144124&extra=page%3D9%26filter%3Dsortid%26sortid%3D311%26searchoption%255B3046%255D%255Bvalue%255D%3D2%26searchoption%255B3046%255D%255Btype%255D%3Dradio&page=1public class InsertNodeToCircularList {ListNode head = null;/* * function to insert a new_node in a list in sorted way. Note that this * function expects a pointer to head node as this can modify the head of * the input linked list */void sortedInsert(ListNode new_node) {ListNode current = head;// Case 1 of the above algoif (current == null) {new_node.next = new_node;head = new_node;}// Case 2 of the above algoelse if (current.data >= new_node.data) {/* * If value is smaller than head's value then we need to change next * of last node */while (current.next != head)current = current.next;current.next = new_node;new_node.next = head;head = new_node;}// Case 3 of the above algoelse {/* Locate the node before the point of insertion */while (current.next != head && current.next.data < new_node.data)current = current.next;new_node.next = current.next;current.next = new_node;}}// Utility method to print a linked listvoid printList() {if (head != null) {ListNode temp = head;do {System.out.print(temp.data + " ");temp = temp.next;} while (temp != head);}}// Driver code to test abovepublic static void main(String[] args) {InsertNodeToCircularList list = new InsertNodeToCircularList();// Creating the linkedlistint arr[] = new int[] { 12, 56, 2, 11, 1, 90 };/* start with empty linked list */ListNode temp = null;/* * Create linked list from the array arr[]. Created linked list will be * 1->2->11->56->90 */for (int i = 0; i < 6; i++) {temp = new ListNode(arr[i]);list.sortedInsert(temp);}list.printList();}}
0 0
- Sorted insert for circular linked list
- Insert into a sorted circular linked list 环状链表 插入节点
- 代码面试题:Insert Sorted Linked List
- Double Circular Linked List
- 【Hackerrank】Insert a node into a sorted doubly linked list
- Insert a node in a sorted linked list.
- Circular linked list and Josephus ring
- 循环链表(Circular Linked List)
- Linked List {singly linked list -> doubly linked list -> circular linked list}
- demo for insert values into sorted single list
- Insert in a sorted list
- Merge two sorted linked list
- 找出circular的linked list的第一个元素
- Circular Doubly Linked List 双向循环链表 C++ 例子
- Insert & delte elements in doublely linked list
- Insert into a Cyclic Sorted List
- Insert into a Cyclic Sorted List
- Insert into a Cyclic Sorted List
- 闭包
- Android内存泄漏分析笔记
- MFC消息映射机制详解
- 源码分析:Java对象的内存分配
- 长连接短连接socket及windows连接数限制配置
- Sorted insert for circular linked list
- android开发中联系人列表显示字母索引
- 栈和队列的实现
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- window常用运行代码
- ubuntu 启动错误stdin:is not tty
- 深度学习笔记3:实现一个卷积神经网络
- Go语言实战_自定义OrderedMap
- 32位和64位C++程序使用ADO访问ORACLE注意事项和区别