【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 ...
随机推荐
- iOS面试知识点
1 iOS基础 1.1 父类实现深拷贝时,子类如何实现深度拷贝.父类没有实现深拷贝时,子类如何实现深度拷贝. 深拷贝同浅拷贝的区别:浅拷贝是指针拷贝,对一个对象进行浅拷贝,相当于对指向对象的指针进行复 ...
- VBA清除Excelpassword保护,2003/2007/2010均适用
Sub Macro1() ' ' Breaks worksheet and workbook structure passwords. Jason S ' probably originator of ...
- [Regular Expressions] Match the Start and End of a Line
We can use: ^: match the beginning $: match the end Let's say we have the string like the following: ...
- 解开Android应用程序组件Activity的"singleTask"之谜
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6714543 在Android应用程序中,可以配 ...
- 获取对象类型(swift)
获取对象类型(swift) by 伍雪颖 let date = NSDate() let name = date.dynamicType println(name) let string = &quo ...
- BOOST 线程完全攻略 - 扩展 - 事务线程
扩展threadtimermoduleexceptionsocket 什么叫事务线程 举个例子: 我们写一个IM客户端的登录子线程,则该子线程会有这么几个事务要处理 No.1 TCP Socket物理 ...
- javascript的本地存储 cookies、localStorage
一.cookies 本地存储 首先是常用的cookies方法,网上有很多相关的代码以及w3cSchool cookies. // 存储cookies function setCookie(name,v ...
- linq和lamda表达式中添加时间判断时解决方案
在工作中遇到个问题,在使用lamda查询数据的时候,需要添加一个时间判断, DateTime.AddDays(3) > e.ExpirationDate 例如:list = Context.Vo ...
- ios 调用相机后 view 下沉问题
我只加了一句代码 现在不报错了 因为这个问题是随机性的 我也不太明白这个地方是怎么回事 我只是这样子做了 问题不出来了 if ([[[UIDevice currentDevice] syst ...
- Code Review中应该关注的点
Magic number/string If statement, you should always use single line or brackets Provide default valu ...