【DataStructure】Some useful methods about linkedList(二)
Method 1: Add one list into the other list.
For example, if list1is {22, 33, 44, 55} and list2 is {66, 77, 88, 99},then append(list1, list2)will change list1to {22, 33, 44, 55, 44, 66, 77, 88, 99}.
- static void append(Node list1, Node list2) {
- if (list1 == null) {
- throw new IllegalArgumentException();
- }
- while (list1.next != null) {
- list1 = list1.next;
- }
- list1.next = list2;
- }
The output is as follows:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3hiMDg0MTkwMTExNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
Method2: Gets a new list combined by two list
For example, if list1 is {22, 33, 44, 55} and list2 is {66, 77, 88, 99}, then concat(list1, list2)will return the new list {22, 33, 44, 55, 44, 55, 66, 77, 88}.
Note that the three lists should be completely independent of each other. Changing one list should have no effect upon the others.
- static Node concat(Node list1, Node list2) {
- Node list3 = new Node(0);
- Node p = list1;
- Node q = list3;
- while(p != null)
- {
- q.next = new Node(p.data);
- p = p.next;
- q = q.next;
- }
- p = list2;
- while(p != null)
- {
- q.next = new Node(p.data);
- q = q.next;
- p = p.next;
- }
- return list3.next;
- }
The output is as follows:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3hiMDg0MTkwMTExNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
Method 3 : Replace the value of element number i with x
void set(Node list, int i, int x)
For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then set(list, 2, 50) will change List to {22, 33, 50, 55, 66, 44, 88, 99}
- Solution 1:
- static void set(Node list, int i, int x) {
- Node p = list;
- int count = 0;
- while (p != null) {
- if (count == i) {
- p.data = x;
- break;
- }
- p = p.next;
- count++;
- }
- }
- Solution2:
- static void set(Node list, int i, int x) {
- if (i < 0) {
- throw new IllegalArgumentException();
- }
- for (int j = 0; j < i; j++) {
- if (list == null) {
- throw new IllegalStateException();
- }
- list = list.next;
- }
- list.data = x;
- }
The output is as follows:
【DataStructure】Some useful methods about linkedList(二)的更多相关文章
- 【DataStructure】Some useful methods about linkedList(三)
Method 4: Gets the value of element number i For example, if list is {22, 33, 44, 55, 66, 77, 88, 99 ...
- 【DataStructure】Some useful methods about linkedList.
/** * Method 1: Delete the input element x * and meanwhile keep the length of array after deleted n ...
- 数据结构之链表(LinkedList)(二)
数据结构之链表(LinkedList)(一) 双链表 上一篇讲述了单链表是通过next 指向下一个节点,那么双链表就是指不止可以顺序指向下一个节点,还可以通过prior域逆序指向上一个节点 示意图: ...
- 基于注解的SpringAOP源码解析(二)
在上篇文章 中我们搭建了一个阅读源码的demo工程,然后简单介绍了一下@EnableAspectJAutoProxy注解,这个注解最重要的功能就是为向Spring中注入了一个beanAnnotatio ...
- 数据结构之链表(LinkedList)(三)
数据结构之链表(LinkedList)(二) 环形链表 顾名思义 环形列表是一个首尾相连的环形链表 示意图 循环链表的特点是无须增加存储量,仅对表的链接方式稍作改变,即可使得表处理更加方便灵活. 看一 ...
- day111:MoFang:邀请好友流程&生成邀请好友二维码&第三方应用识别二维码&本地编译测试&记录邀请人信息
目录 1.邀请业务逻辑流程图 2.邀请好友-前端 3.邀请好友-后端接口(生成二维码) 4.前端获取后端生成的二维码 5.前端长按页面,保存图片到相册 6.客户端通过第三方识别微信二维码,服务端提供对 ...
- Java注解与自己定义注解处理器
动机 近期在看ButterKnife源代码的时候.竟然发现有一个类叫做AbstractProcessor,并且ButterKnife的View绑定不是依靠反射来实现的,而是使用了编译时的注解,自己主动 ...
- Thinking in Java——笔记(17)
Containers in Depth Full container taxonomy You can usually ignore any class that begins with " ...
- Thinking in Java——笔记(11)
Holding Your Objects In general, your programs will always be creating new objects based on some cri ...
随机推荐
- 新手使用ThinkPHP3.2.3的命名空间问题
ThinkPHP3.2.3的命名空间问题 命名空间的出现是为了避免命名冲突. 我们在TP3.2.3的Collection和Model的创建过程中经常会遇到这样的两行代码: 这是在控制器中的写法.其中n ...
- EasyUI 两个日期比较
两个日期进行比较,后一个日期不能早于晚一个日期.需要自己去扩展validatebox的方法 1.对比2个密码是否相同 $.extend($.fn.validatebox.defaults.rules, ...
- HDU 1827 Summer Holiday(Tarjan缩点)
Problem Description To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity ...
- Swift——(一)为Swift内置类型加入属性
在看苹果官方的Swift Language的时候,遇到实验:Write an extension for the Double type that add an absoluteValue prope ...
- StackOverflow程序员推荐:每个程序员都应读的30本书
“如果能时光倒流,回到过去,作为一个开发人员,你可以告诉自己在职业生涯初期应该读一本,你会选择哪本书呢?我希望这个书单列表内容丰富,可以涵盖很多东西.” 很多程序员响应,他们在推荐时也写下自己的评语. ...
- html5 音频
目前,web页面上没有标准的方式来播放音频文件,大多数的音频文件是使用插件来播放,而众多的浏览器使用了不同的插件.而html5的到来,给我们提供了一个标准的方式来播放web中音频文件,用户不再为浏览器 ...
- SELECT TOP column FROM table [ORDER BY column [DESC]]
如果想返问表中行的子集,仅需要返回特定数量的记录,而不管符合条件的行有多少.要返回排在前面的值,可以有两个选择:指定固定数量的行,或者指定总行数的百分比.SQL Server不对这些数据做任何分析,共 ...
- 2014年1月9日 Oracle常见授权与权限回收[转]
1.GRANT 赋于权限 常用的系统权限集合有以下三个: CONNECT(基本的连接), RESOURCE(程序开发), DBA(数据库管理) 常用的数据对象权限有以下五个: ALL ON 数据对象名 ...
- 改变navigationbar的底部线条颜色
[[UINavigationBar appearance] setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault]; CG ...
- VS2015预览版中的C#6.0 新功能(三)
VS2015预览版中的C#6.0 新功能(一) VS2015预览版中的C#6.0 新功能(二) Using static 使用using StaticClass,你可以访问StaticClass类里的 ...