【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 ...
随机推荐
- java技术学习网址收藏
Bootstrap:http://www.runoob.com/bootstrap/bootstrap-intro.html AngularJS : http://www.runoob.com/ang ...
- Monkey Tradition(中国剩余定理)
Monkey Tradition Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Submi ...
- PHP你可能也会掉入的坑
今天被人问: $var = 'test'; if (isset($var['somekey'])) { echo 'reach here!!!'; } 会不会输出'reach here!!!'? -- ...
- IDataParameter调用存储过程
public string GenerateExamePaper(string paperType, string driverID, string MacAddr) { ...
- vs连接mysql出错解决方法
vs连接mysql出错解决方法 先按以下的步骤配置一下: **- (1)打开VC6.0 工具栏Tools菜单下的Options选项.在Directories的标签页中右边的"Show dir ...
- Hacker(六)----黑客藏匿之地--系统进程
windows系统中,进程是程序在系统中的依次执行活动.主要包括系统进程和程序进程两种. 凡是用于完成操作系统各种功能的各种进程都统称为系统进程: 而通过启动应用程序所产生的进程则统称为程序进程. 由 ...
- [HeadFrist-HTMLCSS学习笔记][第一章Web语言:开始了解HTML]
head title body 元素= 开始标记 + 内容 +结束标记 还能给段落一个变量名 <p id="houseblend"> body </p> s ...
- 15. SSH 远程
一.原理: 使用SSH连接Centos时,我们可以创建一个公钥和一个私钥,公钥放在服务端,私钥放在客户端,当客户端去连接服务端时,会先去查找密钥, 要是客户端的私钥可以和服务端的公钥匹 ...
- 《JavaScript 闯关记》之垃圾回收和内存管理
JavaScript 具有自动垃圾收集机制(GC:Garbage Collecation),也就是说,执行环境会负责管理代码执行过程中使用的内存.而在 C 和 C++ 之类的语言中,开发人员的一项基本 ...
- 经典SQL语句大全(转载)
原文http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html#top 一.基础 1.说明:创建数据库CREATE DATABAS ...