Simple algorithm-Find the midpoint of the list and the penultimate K-node (common method and fast pointer method overhead)

Source: Internet
Author: User

1.1.1 Intermediate nodes of a unidirectional
list

Idea one

Go through the list first, get the list length, find the position of the middle node, and then go through the list head until the middle node is located.

The complexity is probably O (3/2n),

Idea two

Set two pointers to traverse from the head node of the chain, where one pointer goes down 2 nodes at a time, one pointer goes down one node at a time, and when the fast pointer reaches the last, the slow pointer just reaches the middle node.

The complexity is probably O (n)

comparison of two ideas

The idea is to let a person to find a certain distance of the middle position, the idea of two like to let two people to find the middle position of a certain distance.

Thought one, this person needs to walk 1.5 times times to be able to find the middle point (assuming he arrives at the finish line can immediately arrive the starting point).

Train of thought two, a person walked through the whole process, another person walked half a way, two people add up is also 1.5 times times the distance.

So, in this sense, the two ways of spending are actually the same.

1.1.1 Find the Countdown k node

Idea one

Go through the list first, get the link table length, find the position of the penultimate K, and then start traversing from the list head, so we have a total of (2l-k) nodes.

Idea two

And find the middle point of thinking a little bit the same, just let a pointer go first k nodes, and then two pointers each time a node, when the front of the pointer to reach the end point, the back of the pointer is exactly at the bottom of the K node. Therefore also altogether walked (2l-k) a node.

So the complexity of the two methods is the same.

The code is as follows:

/* * * introduction:reverse list * Author:gykimo * date:20121212 * */#include <stdio.h> #include <
	malloc.h> typedef struct node{int data;
struct node* next;

}node;		#define List_len 12//List length Node * LIST = NULL; Linked list//find the middle point of the list void Find_median_ite () {node * fast = list;//Two nodes per walk node * slow = list;//one node at a time (fast! = NULL)
		{fast = fast->next;

		if (fast = = NULL) break;

		Fast = fast->next;
	slow = slow->next;
} printf ("The Media is%d\n", slow->data);
	} void Find_one_reverse (int seq) {Node * first = List;
	Node * second = List;

	int first_steps = seq;
		First, go to SEQ node while (first_steps>0) {first = NULL && * = = = first->next;
	first_steps--;
		}//Description list length is less than seq, so there is no penultimate seq node if (first_steps > 0) {printf ("The%d node does not exist\n", seq);
	Return
		} while (first! = NULL) {first = first->next;
	Second = second->next;
} printf ("The%d node is%d\n", seq, second->data); }//Generate linked list VOID make_list () {list = (node *) malloc (sizeof (node) * List_len);
	int i = 0;
		for (i = 0; i < (list_len-1); i++) {(LIST + i)->data = i + 1;
	(list + i)->next = list + i + 1;
	} (List + list_len-1)->data = List_len;
(List + list_len-1)->next = NULL;
	} void Print_list () {Node * cur = list;
		while (Cur!=null) {printf ("%d", cur->data);
	Cur = cur->next;
} printf ("\ n");
	} int main () {make_list ();
	Print_list ();
 Find_median_ite ();//Find the Middle node find_one_reverse (4);//Find the bottom 4th node}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.