【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:

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:

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(二)

时间: 2024-08-28 02:12:56

【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}, then get(list, 2) will return 44. Solution 1: static int get(Node list, int i) { if (i < 0) { throw new IllegalArgumentException(); } for (int j =

【DataStructure】 Five methods to init the List in java

Do you know how to init list in other way except for new object? The following will give you serveral tips. If having other great idea, you are welcome to share. [java] view plaincopy import java.util.ArrayList; import java.util.Arrays; import java.u

Some useful methods about linkedList.

/** * Method 1: Delete the input element x * and meanwhile keep the length of array after deleted n * @param a  the array * @param n  the length of array after deleted. * @param x  the element that need to be deleted. */ static void delete(int[] a, i

【DataStructure】Some useful methods about linkedList.

/** * Method 1: Delete the input element x * and meanwhile keep the length of array after deleted n * @param a  the array * @param n  the length of array after deleted. * @param x  the element that need to be deleted. */ static void delete(int[] a, i

数据结构之链表(LinkedList)(三)

数据结构之链表(LinkedList)(二) 环形链表 顾名思义 环形列表是一个首尾相连的环形链表 示意图 循环链表的特点是无须增加存储量,仅对表的链接方式稍作改变,即可使得表处理更加方便灵活. 看一样著名的应用场景 我们就可以用环形单链表解决这个问题. 首先我们怎么构建一个环形链表 分析: 1. 先创建第一个节点, 让 first 指向该节点,并形成环形 2. 后面当我们每创建一个新的节点,就把该节点,加入到已有的环形链表中即可. 示意图: 代码: // 创建一个Boy类,表示一个节点 cla

Swift -- 中文版两大官方文档汇总

Swift官方文档由CocoaChina翻译小组精心翻译制作而成,目前两本文档中文版已全部完成!在此,我们对所有参与的译者.组织人员以及工作人员表示衷心的感谢!本文为您提供两本文档的在线阅读以及下载!请多多关注Swift!!多多关注CocoaChina!!! The Swift Programming Language 欢迎使用Swift (一)关于Swift--About Swift (二)Swift 初见--A Swift Tour Swift -- 语言指南 (一)基础部分 -- The

[数据结构]基本概念2

逻辑结构:数据之间的关系.常见的逻辑结构只有两种,即[线性结构]和[非线性结构] 物理结构:在计算机中的存储方法.常见的物理结构只有两种,即[顺序存储]和[链式存储] 常见数据结构: 一.线性表 1.线性表的逻辑结构: a.有且只有一个开始结点a1,没有直接前驱,有唯一的直接后继a2 b.有且只有一个终端结点an,没有直接后继,有唯一的直接前驱an-1 c.剩余的内部结点都有唯一的直接前驱和直接后继 2.线性表的操作: a.创建线性表 b.获取元素个数 c.随机获取某一个元素 d.插入 e.删除

从一个实例详解敏捷测试的最佳实践

简介: 敏捷软件开发是目前十分流行,并在业界逐步推广的软件开发模式.不同与传统的软件开发模式,敏捷开发模式有着自己鲜明的价值和方法.其中,敏捷测试部分也同以往的软件测试流程有所不同.这对测试人员提出了新的要求,带来了新的挑战.本文将结合一个软件项目实例,基于项目开发的不同阶段,详细介绍每个阶段的主要测试活动.文中将分析每个主要测试活动的前提条件和目标任务,并根据实例推荐最佳的解决方案. 第一部分:敏捷软件开发简介 敏捷软件开发(Agile Software Development)初起于九十年代

swift网址

http://www.cocoachina.com/industry/20140613/8818.html Swift -- 中文版两大官方文档汇总发布于:2014-06-13 15:34阅读数:22081Swift官方文档由CocoaChina翻译小组精心翻译制作而成,目前两本文档中文版已全部完成!在此,我们对所有参与的译者.组织人员以及工作人员表示衷心的感谢!本文为您提供两本文档的在线阅读“”阅读器Swift官方文档 Swift官方文档由CocoaChina翻译小组精心翻译制作而成,目前两本