博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linkedlist 最难题 Insert into a Cyclic Sorted List
阅读量:6364 次
发布时间:2019-06-23

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

Given a node from a cyclic linked list which has been sorted, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be any single node in the list.

把所有edge case和 通常case写出来,题基本上也做出来了!

 

 

EDIT:
Thanks to dear readers  and  who pointed out my mistake. When the list has only one value, inserting a different value would be handled by case 3), not case 1). Besides, I believe I did not explain “What is a cyclic sorted list” nicely, as this had caused some confusion. Imagine you have a sorted list, but its tail points back to its head. In other words, the list must have a minimum node, continue in a non-descending order, and eventually points back to this minimum node itself. And the only way to access the list is via aNode, which can point to any node in the list and does not necessarily point to the minimum node.

First, it is important that you understand what a cyclic linked list is. A cyclic linked list differs from a normal linked list in that its tail node points back to its head node instead of NULL.

This problem seems a little tricky because the given node is not necessarily the list’s head (ie, the node that has the smallest element). It shouldn’t take you too long to come up with an idea, but beware. There are hidden traps around the corner and you are bound to make some mistakes if you are not careful in your thoughts.

A cyclic sorted linked list. Note that the tail is pointing back to its head. The only reference to the list is a given node which can be any node in the list. Let’s say that you need to insert 4 into the list.

 

This is how the cyclic list becomes after inserting 4. Note that the cyclic linked list remained in sorted order.

 

Hints:

It is best to list all kinds of cases first before you jump into coding. Then, it is much easier to reduce the number of cases your code need to handle by combining some of them into a more generic case. Try to also list down all possible edge cases if you have time. You might discover a bug before you even start coding!

Solution:

Basically, you would have a loop that traverse the cyclic sorted list and find the point where you insert the value (Let’s assume the value being inserted called x). You would only need to consider the following three cases:

    1. prev→val ≤ x ≤ current→val:
Insert between prev and current.
  1. x is the maximum or minimum value in the list:
    Insert before the head. (ie, the head has the smallest value and its prev→val > head→val.
  2. Traverses back to the starting point:
    Insert before the starting point.

Most people have no problem getting case 1) working, while case 2) is easy to miss or being handled incorrectly. Case 3), on the other hand is more subtle and is not immediately clear what kind of test cases would hit this condition. It seemed that case 1) and 2) should take care of all kinds of cases and case 3) is not needed. Think again… How can you be sure of that? Could you come up with one case where it hits case 3)?

Q: What if the list has only one value?
A: 
Handled by case 1). Handled by case 3).
Q: What if the list is passed in as NULL?
A: Then handle this special case by creating a new node pointing back to itself and return.
Q: What if the list contains all duplicates?
A: Then it has been handled by case 3).

Below is the code. You could combine both negation of case 1) and case 2) in the while loop’s condition, but I prefer to use break statements here to illustrate the above idea clearer.

 

 

转载于:https://www.cnblogs.com/leetcode/p/3309099.html

你可能感兴趣的文章
event事件
查看>>
js获取微信code
查看>>
PHP输出中文乱码的解决方法
查看>>
Intellisense for the NHibernate XML Schemas
查看>>
dart 异步事件执行流程分析(二)
查看>>
windows平台升级ORACLE11.2.0.1到11.2.0.4
查看>>
Task list 20130311
查看>>
当传统企业遇上大数据
查看>>
ubuntu16.04 开启ipv6支持
查看>>
TCP/IP协议栈 --- 网络层(IP 首部 和分片)
查看>>
<script language="JavaScript"> or <script type="text/javascript"> ?
查看>>
Win7 VS2019安装后创建C++工程失败解决
查看>>
stormconf.ser does not exist
查看>>
显示ID对应的值
查看>>
IOS 面试题总结
查看>>
iOS 专题 之 界面开发 之 控件
查看>>
win10安装与配置JDK8的环境变量
查看>>
比赛之字典树题解
查看>>
poj3250(单调栈模板题)
查看>>
W3wp.exe
查看>>