【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 ...
随机推荐
- 【多线程】--生产者消费者模式--synchronized版本
在实现生产者消费者模式之前,我们先了解一下线程的5种状态:被创建.运行.冻结.消亡.阻塞,如下图: 在Jdk1.5发布之前,我们实现生产者消费者模式一般使用synchronized + while循环 ...
- Jpeg(模拟)
Jpeg Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- JavaScript ----------- 组合继承
继承 实现继承:继承实际的方法.ECMAScript 只支持实现继承,而且其实现基础主要是依靠原型链来实现的. 基本思想是:利用原型来实现一个引用类型继承另外一个引用类型的属性和方法. 原型 - 构造 ...
- jquery简单切换插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- T4模板_根据DB生成实体类
为了减少重复劳动,可以通过T4读取数据库表结构,生成实体类,用下面的实例测试了一下 1.首先创建一个项目,并添加文本模板: 2.添加 文本模板: 3.向T4文本模板文件添加代码: <#@ tem ...
- iOS 面试题 3
0.请写出代码,用blocks来取代上例中的protocol,并比较两种方法的优势.实际应用部分?请写出代码,用blocks取代协议或回调方法 声明: #import <Foundation/F ...
- UVA 10253 Series-Parallel Networks (树形dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Series-Parallel Networks Input: standard ...
- activiti笔记三 Activiti问题重现
测试的时候出现一个异常: ContextLoader.initWebApplicationContext(308) | Context initialization failed org.spring ...
- JS创建类和对象(好多方法哟!)
http://www.cnblogs.com/tiwlin/archive/2009/08/06/1540161.html 这是别人写的~~~我借来看看 JavaScript 创建类/对象的几种方式 ...
- js 对日期加减
function getDate(days) { var now = new Date(), newDate = new Date(now.getTime() - 86400000 * days), ...