How to find the penultimate element in a single-linked list
(1) Method 1: First through the single-linked list, the entire single-linked list of the length of N, and then the penultimate K, converted to a positive number of n-k, then go through one time to get the results. However, the algorithm requires two traversal of the linked list, the first traversal is used to solve the single-linked list length, and the second traversal is used to find the positive number of the n-k element.
(2) Method 2: If you go in the direction from beginning to end, starting with an element in the list, traversing the K-element, just reaching the tail of the chain, then the element is the reciprocal K-element to find. According to this nature, the following algorithm can be designed: starting from the head node, each node element of the linked list is tested at once, traversing the k elements to see if the end of the chain is reached until the last K element is found. This method will iterate over the same batch of elements repeatedly, for most elements of the list, to traverse the k elements, if the length of the list is n, the algorithm has a time complexity of O (KN) level, the efficiency is too low.
(3) Method 3: There is a more efficient way to find the last K elements in a single traversal. Since the single-linked list can only access the nodes of the linked list from beginning to end, if you want to find the penultimate element of the linked list, you can only iterate through the lookup from beginning to end. During the lookup process, set two pointers so that one pointer moves k-1 than the other, and the two pointers move forward at the same time. Loop until the first pointer is null, the position the other pointer refers to is the location you want to find. As shown below:
#include <stdio.h> #include <stdlib.h> typedef struct Node {int data;
struct node *next;
} NODE; The tail interpolation method creates a single-linked list (the lead node) node *createend (int arr[], int len) {node *head = (node *) malloc (sizeof (node));//Generate head node head-&
Gt;next = NULL; NODE *end = head;
Tail pointer initialization for (int i = 0; i < len; i++) {Node *p = (node *) malloc (sizeof (node));//create a node for each array element
P->data = Arr[i]; End->next = p;
The node p is inserted into the endpoint after end = p; } end->next = NULL;
Once the single-linked list is established, the pointer field of the endpoint is empty to return head;
}//Find the reciprocal k element in a single-linked list node *search_last_k (node *head, int k) {node *p1 = head;
NODE *p2 = head;
for (int i = 0; i < K; i++) {P2 = p2->next;
} while (P2! = NULL) {p1 = p1->next;
P2 = p2->next;
} return p1;
}//single-linked list print void print (NODE *head) {if (head = = NULL) return;
NODE *p = head->next; while (P! = NULL) {printf ("%d\n", p->data);
p = p->next;
}} int main (void) {int arr[] = {1,2,3,4,5,6,7};
int len = sizeof (arr)/sizeof (int);
NODE *head = Createend (arr, Len);
Print (head);
printf ("-----------\ n");
NODE *temp_k = Search_last_k (head, 3);
printf ("%d\n", temp_k->data);
return 0; }