Single linked common title (head appreciated interpolation)

We spend the next code is a realization, click on a brief introduction and implementation of a single list CRUD

Seeking a number of active nodes

    /*
	 * 求单链表有效长度(如果是带头结点的链表,需求不统计头节点)
	 */
	public int getLength() {
		if (head.next == null) {//空链表
			return 0;
		}
		int length = 0;	
		//定义一个辅助的变量, 这里我们没有统计头节点
		HeroNode cur = head.next;
		while (cur != null) {
			length++;
			cur = cur.next;
		}
		return length;
	}

2 single linked list to find the k-th node countdown

Here we need to use an effective method for the number of nodes above.
Thinking
1. Preparation of a method, the receiving head node while receiving an index
2. index represents the index is the penultimate node
3. The above getLength () method to obtain the list of effective total length of size
4. size obtained, from our the first start traversing the list (size-index) number, you can get
5. If found, the node is returned, otherwise nulll

public HeroNode getBackKey(int indax) {
		//判断如果链表为空,返回null
		if (head.next == null) {
			return null;
		}
		//第一个遍历得到链表的长度(节点个数)
		int Size = getLength();
		//第二次遍历  size-index 位置,就是我们倒数的第K个节点
		//先做一个index的校验
		if (indax <= 0 || indax > Size) {
			return null;
		}
		//定义给辅助变量, for 循环定位到倒数的index
		HeroNode cur = head.next;
		for (int i = 0; i < Size - indax; i++) {
			cur = cur.next;
		}
		return cur;
	}

3 inverted single linked list (the first interpolation)

Personal thoughts (for reference only, if wrong, please point out):
Here Insert Picture Description
the first node of the first off the original list, it will be the first node to the new list (in this case the first node in the new list is empty), then the new list points to the first node (the first node in the list at this time a new linked list for the original first node), then the cur after the shift, the shift cur original list of the second node, disconnecting the original list of two nodes, the first node pointing to a new linked list, the new list head pointer pointing to the original list of the second node is disconnected, and so on.

------------------------------------
soul painter mind map:
Here Insert Picture DescriptionHere Insert Picture Description
This is why use a next = cur .next be stored at a current node, if the absence of the next, will be turned off when a list operation.

public void reversetList() {
		if (head.next == null || head.next.next == null) {
			return;
		}
		HeroNode cur = head.next;
		HeroNode next = null;

		HeroNode reversetListHead = new HeroNode(0, "", "");

		while (cur != null) {
			next = cur.next;//先暂时保存当前节点的下一个节点,因为后面需要使用		
			cur.next = reversetListHead.next;//将cur与后面链表断开,指向新链表的第一个节点
			reversetListHead.next = cur; 	//将cur连接到新的链表上
			cur = next;//让cur后移
				
		}
		// 将head.next 指向 reverseHead.next , 实现单链表的反转
		head.next = reversetListHead.next;
	}

Single chain printing head 4 from the tail

Stack features: last out.
Idea: Each node added to the stack, through the advanced features to achieve the effect of a reverse print.

/*
	 * 4 单链表通过栈反转
	 */
		public void reversetStackPrint() {
			if(head.next==null) {
				return;
			}
			//创建要给一个栈,将各个节点加入栈
			Stack<HeroNode>stack=new Stack<HeroNode>();
			HeroNode cur=head.next;
			//将链表的所有节点加入栈
			while(cur!=null) {
				stack.add(cur);
				cur=cur.next;
			}
			//将栈中的节点进行打印,pop 出栈
			while(stack.size()>0) {
				System.out.println(stack.pop());
			}
		}
Published 28 original articles · won praise 1 · views 380

Guess you like

Origin blog.csdn.net/weixin_44824500/article/details/104552930