【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 ... 
随机推荐
- C# 制作 仪表
			以前在百度写的文档,转移到此处 前些天在做NetAnalyzer时,需要使用一个指针仪表,网上看了一下,也有人做过,但是大部分都是收费的,本着自力更生的原则,于是决定自己设计一个,今天拿出来有读者分享 ... 
- python学习之路-9 socket网络编程
			socket基础 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. so ... 
- 单调队列-Hdu-4122-Alice's mooncake shop
			题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4122 题目意思: 一家月饼店,有n个订单,从2001年1月1日0时开始24小时营业开m个小时,且每个 ... 
- 解决angular 与django的冲突
			{% block main %} <script type="text/javascript" src="http://cdnjs.cloudflare.com/a ... 
- 关于Latch
			Latch是什么 Latch是SQL Server引擎保证内存中的结构的一致性的轻量同步机制.比如索引,数据页和内部结构(比如非叶级索引页).SQL Server使用Buffer Latch保护缓冲池 ... 
- 观察者模式与Boost.Signals
			1) 观察者模式定义 略,各种设计模式的书上都有定义. 2) 观察者模式一般实现 观察者模式一般实现,都是“被观察者”保存一个“观察者”的列表,循环这个列表来通知“观察者”.代码,其中使用了b ... 
- Android 打造自己的个性化应用(二):应用程序内置资源实现换肤功能
			通过应用程序内置资源实现换肤,典型的应用为QQ空间中换肤的实现. 应用场景为: 应用一般不大,且页面较少,风格相对简单,一般只用实现部分资源或者只用实现背景的更换. 此种换肤方式实现的思路: 1. 把 ... 
- ListHelper
			using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ... 
- C#解决MDI窗体闪屏的方法
			最近从师兄手上接了一个C#的项目,需要用到MDI窗体,可是每当我显示子窗体的时候会有一次“闪烁”,很明显,看起来非常不爽,查找许久,知道是每次在show()子窗体的时候都会调用子窗体构造函数重绘窗体, ... 
- Android 多线程断点下载(非原创)
			1.服务器的CPU分配给每条线程的时间片相同,服务器带宽平均分配给每条线程,所以客户端开启的线程越多,就能抢占到更多的服务器资源,这里在客户端开启多个线程来从服务器下载资源 2.fragment_ma ... 
