The number of active nodes in a single linked list
Analysis of ideas:
If the lead is a list of nodes, the nodes do not need to head statistics. That is the length of the list.
- Determining whether the list is empty, if empty, the process directly returns 0;
- Given a variable length, the number of records for the nodes;
- Is not empty, by a temporary variable pointing to the next position of the head node,
- Traverse the linked list, as long as data is not empty, length plus one. Until the next temp of an empty date.
- The variable return
Code:
public static int getLength(HeroNode head){
if(head.next == null){ //空链表
return 0;
}
int length = 0;
//定义一个辅助变量,直接指向头节点的下一个节点
HeroNode cur = head.next;
while(cur != null){
length++;
cur = cur.next;//遍历
}
return length;
}
Find the reciprocal k-th node in the single list
Analysis of ideas:findLastIndexNode(Node node , int index)
- First determine whether the list is empty, empty return null;
- The total length is not empty, the first traverse, the output of the list
- Set a temporary variable temp is used to traverse operation, temp is a head node from the beginning of the head
- Defines a length size for storing the list;
- As long as the temp is not empty, size is incremented until a complete traversal of the list
- Legitimacy judgment index is not equal to 0 less than or greater than the total length of the list
- And then recycled to the size-index for time, the position of the target point temp;
- The return node
Code:
public HeroNode findLastIndexNode(Node node,int index) {
// 判断链表是否为空,
if (node == null) {
return null;
}
// 不为空,则先进行遍历,输出链表的总长度
// 定以一个临时变量temp用来进行遍历操作,
HeroNode temp = head.next;//从链表头部的下一个开始
// 定义一个size用来存储链表的长度
int size = 0;
while (temp != null) {// temp下一个节点还有值
size++;
temp = temp.next;//指向下一个节点继续
}
System.out.println(size);
// 得到链表的总长度size后,我们只需遍历size-index次即可得到第K个元素
HeroNode cur = head.next;
// 保证输入的index是有效的
if(index <= 0 || index > size){
return null;
}
for(int i = 0; i< size-index;i++){
cur = cur.next;
}
return cur;
}
Singly linked list reversal
Analysis of ideas:
- Define a new header node, reverseHead
- To traverse from beginning to end of the original list, each node traversed, taken out, put back reverseHead node
- The original head .next point reverseHead.next
Code:
public static void reverseLinkedList(HeroNode node) {
// 1. 判断所给的链表的长度是0或是1不用反转
if(node.next == null || node.next.next == null){
return;
}
//2. 定义一个新的头节点,reverseHead
HeroNode reverseHead = new HeroNode(0,"","");
//3. 从头到尾去遍历原来的链表,每遍历一个节点,将其取出,放到reverseHead节点的后面
HeroNode cur = node.next; // 临时变量用来进行遍历操作
HeroNode next = null; //用来存储当前节点的下一个节点的位置
while(cur != null) {
// 从右往左进行插入,
next = cur.next; //
cur.next = reverseHead.next; // 如果temp此时是节点2,那么reverseHead.next是节点1,我们先让节点2的下一位指向节点1
reverseHead.next = cur; //让新链表的头结点指向取出来的节点。把temp插到reverseHead后面
cur = next; // 继续
}
//4. 将原来的头部.next指向reverseHead.next
node.next = reverseHead.next;
}s
Single chain print head from the tail
Mode 1: Reverse traversal
Then the first reverse traversal, will destroy the linked list structure
Mode 2: Stack Stack
Analysis of ideas:
The use of the advanced features of the stack, each node will be pressed into the stack, and then use the stack, to achieve the effect of the reverse printing
- First determine whether a given list is empty, empty direct return
- Is not empty, create a stack to define a temporary variable traverse the list, the node will push
- As long as the stack is not empty, last-out popped pop ()
Code:
public static void reversePrint2(HeroNode node){
//1.
if(node.next == null){
return;
}
//2.
HeroNode cur = node.next;
Stack<HeroNode> stack = new Stack<HeroNode>();
while(cur != null){
stack.push(cur);
cur = cur.next;
}
// 3.
while(!stack.empty()){ //或者stack.size()>0
System.out.println(stack.pop());
}
}