Data structure - singly linked list related operations

1: Related operations of singly linked list

 1.1: Add an element to a singly linked list

 1.2: Singly linked list deletes the element at the specified position ( deletes an element )

 1.3: Singly linked list printing

 1.4: Inversion of a singly linked list

 1.5: Singly linked list to find the element of the k-th node from the bottom

 1.6: Print a singly linked list from end to end

 1.7: Find the value of an intermediate node in a singly linked list

Two: code (java implementation)

2.1: Construct a node, equivalent to a structure in Java

	/**
	 * Equivalent to struct class
	 */
	class ChainNode {
		int data;
		ChainNode next;

		public ChainNode(int data) {
			this.data = data;
		}
	}

2.2: Add an element to a singly linked list

/**
	 * linked list head node
	 */
	private ChainNode head = null;

	public void setHead(ChainNode head) {
		this.head = head;
	}

	public ChainNode getHead() {
		return this.head;
	}

	/**
	 * Add data to the linked list
	 */
	public void addNode(int data) {
		ChainNode node = new ChainNode(data);
		if (head == null) {
			head = node;
			return;
		}
		ChainNode temp = head;
		// move the pointer to the tail node
		while (temp.next != null) {
			temp = temp.next;
		}
		// insert from the tail node
		temp.next = node;
	}

2.3: Output the contents of the linked list from beginning to end

	/**
	 * Output linked list data
	 */
	public void printList() {
		ChainNode node = head;
		while (node != null) {
			System.out.print(node.data + ",");
			node = node.next;
		}
	}

2.4: Output the contents of the linked list from the end to the beginning

	/**
	 * print nodes from end to beginning
	 *
	 * @param head2
	 *Step: 1: Use recursion, in C language we generally use stack to solve
	 * 2: Traverse the linked list from the beginning to the end and put it on the stack, and read it from the stack
	 */
	private void printReverseList(ChainNode headNode) {
		if (headNode != null) {
			printReverseList(headNode.next);
			System.out.print(headNode.data + ",");
		}

	}

2.5: Delete the node at the specified location

	/**
	 * The linked list deletes the data of a specified node
	 */
	private boolean deleteNode(int index) {
		// The array table below is less than 0 or greater than the length of the array
		if (head == null || index < 0 || index > length()) {
			return false;
		}
		if (index == 1) {
			// delete the head node
			head = head.next;
		}
		// delete non-head nodes
		int count = 2;
		ChainNode preNode = head;
		ChainNode curNode = preNode.next;

		while (curNode != null) {
			if (count == index) {
				preNode.next = curNode.next;
				curNode.next = null;
				return true;
			}

			// coordinate backward
			preNode = curNode;
			curNode = curNode.next;
			count++;
		}
		return false;
	}

2.6: Implement the inversion of the linked list

/**
	 * Realize the inversion of the linked list
	 * Step 1: Use three pointers preNode, currentNode, nextNode, next to temporarily store the next node
	 */
	private void reverserNode() {
		if (head == null) {
			return;
		}
		ChainNode preNode = head;
		ChainNode curNode = head.next;
		ChainNode nextNode = null;
		while (curNode != null) {
			// temporarily save the next node
			nextNode = curNode.next;
			// The current node points to the previous node
			curNode.next = preNode;
			// move pointer backward
			preNode = curNode;
			curNode = nextNode;
		}
		head.next = null;
		this.head = preNode;
	}

2.7: Find the k-th last node in a singly linked list

	 * Find the K-th position from the bottom in the singly linked list
	 * Steps:
	 * 1: Set two pointers, the first pointer is K steps earlier than the second pointer,
	 * 2: The first pointer reaches the end. The second pointer is just at the Kth position
	 *
	 */
	private ChainNode findInverseNode(int k) {
		if (head == null || k < 0 || k > length()) {
			return null;
		}
		ChainNode frontNode = head;
		ChainNode rearNode = head;
		for (int i = 0; i < k; i++) {
			rearNode = rearNode.next;
		}
		while (rearNode.next != null) {
			frontNode = frontNode.next;
			rearNode = rearNode.next;
		}
		return frontNode;
	}

2.8: Find the value of the intermediate node in the linked list

/**
     * Find the middle node of a singly linked list
     * Ideas:
     * Set two working pointers, frontNode takes one step at a time, rearNode takes two steps at a time
     * When rearNode reaches the end of the linked list, frontNode points to the middle node of the singly linked list
     * Note:
     * When the number of linked lists is odd, frontNode points to the middle node
     * When the number of linked lists is even, the node pointed to by frontNode and frontNode. next is the intermediate node of the linked list
     */
	private ChainNode findMidNode() {
		ChainNode frontNode = head;
        ChainNode rearNode = head;
        while(rearNode != null && rearNode.next != null && rearNode.next.next != null){
            frontNode = frontNode.next;
            rearNode = rearNode.next.next;
        }
        return frontNode;
	}
Three: source code download








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324704278&siteId=291194637