Single linked list Knowledge Point Learning--android Foundation Series (I.)

Source: Internet
Author: User

Well, look at other people finishing, their own notes, the topic is a common must test, Hee hee, single chain


The basic knowledge of the table I will not write, too many people write.




Look before!!!!!!!. involved in C knowledge more, C knowledge I will later consider whether to write an additional article


can assist in future Android Essential Foundation Series blog post


struct ListNode
{
int data;
ListNode * NEXT;

};




1. Find the number of nodes in the chain list
Idea: If the list is empty, return 0; Loop to determine if the next pointer in the list is empty, no, length + + (initialize with unsigned int length = 0), until the next is null, return length


The number of nodes in the single linked list  unsigned int getlistlength (ListNode * phead)  {   if (Phead = NULL)    return 0;     unsigned int mlength = 0;   ListNode * pcurrent = phead;   while (pcurrent! = NULL)   {    mlength + +;    Pcurrent = pcurrent->m_pnext;   }   return mlength;  }  

2. Reverse the single-linked list
Idea: Traverse the original list from beginning to end, each traversing a node, and take it off the top of the new list.


Note that the list is empty and only one node is in the case. Time complexity is O (n); (This is Method 2)
How do I reverse a single linked list?
Method 1: Store the single-linked list as an array, and then reverse the index in reverse order.
Method 2: Use 3 pointers to traverse the single-linked list, reversing the link-by-node.
Method 3: From the 2nd node to the nth node, after each node is inserted into the 1th node (head node)


, and finally move the first node to the end of the new table.
Method 4: Recursive (believe that we are familiar with the point is that for most of the tree problems, can basically consider


To be resolved by hand. But what we're not familiar with is that for some problems with single-linked lists, you can also make


with recursion. You can think of a single linked list as a tree that is always left (right) subtree, so you can consider recursion


To solve. Or, because the structure of the single-linked list itself also has self-similar characteristics, so you can consider the use of recursive


return to solve)
http://blog.csdn.net/feliciafay/article/details/6841115


Put the code on,

listnode* reverselist (listnode* head)  {     if (null==head| | Null==head->next)         return head;    Less than two nodes are not necessarily reversed.   listnode* p = head;   listnode* q = head->next;   listnode* R;      Head->next = NULL;        The old head pointer is a new tail pointer, and next needs to point to null while      (q) {          R = q->next;//Hold the next step to process the pointer          q->next = p;//Then the P Q alternately works in reverse          p = q;           Q = r;       }   Head=p; The last q must point to null, so return p as the new head pointer      return head;        

3. Find the reciprocal k nodes in a single linked list
The most common approach is to count the number of nodes in a single linked list before finding the N-k node. Note


The meaning of the list is empty, K is 0,k for 1,k is greater than the number of nodes in the list of cases. Time Complexity of O (n)


。 Code slightly.
Here is another way of thinking, which is also used in other topics.
The main idea is to use two pointers, first let the front of the pointer to the positive K node, so that before and after two.


The distance between the pointers is K-1, followed by two pointers forward, and the front pointer goes to the last knot


Point, the node at the back of the pointer is the bottom-K node.




Actual types
A single-linked list with header nodes is known, and the node structure is


Suppose that the list is given only by the head pointer. If you do not change the list, please design one as high as possible


Algorithm to find the node (k is a positive integer) in the penultimate position of the list. If the search is successful, count


The value of the data field of the node is output and returns 1; otherwise, only 0 is returned. Requirements:
1) Describe the basic design idea of the algorithm.
2) Describe the detailed implementation steps of the algorithm.
3) According to the design idea and the implementation steps, to use the programming language to describe the algorithm (using C, C + + or Java language


Key points, please give a brief comment.
Question Answer:
1) The basic design idea of the algorithm is as follows: (thought is actually another idea on top)
The key to the problem is to design an algorithm that is as efficient as possible, by traversing through a list of links, to find the countdown K


The location of the node. The basic design idea of the algorithm is: Define two pointer variables p and q, initially pointing to the head knot


The next node of the point (the first node of the list), the P pointer moves along the linked list, and when the P pointer moves to K


node, the Q pointer begins to move synchronously with the P pointer.
When the P pointer moves to the last node, the Q pointer indicates that the node is the reciprocal K-node. The above process


The linked list is scanned only once.




2) The detailed implementation steps of the algorithm are as follows:
①count=0,p and Q point to the next node of the linked list header node.
② if P is empty, turn ⑤.
③ if Count equals K, then Q points to the next node; otherwise, count=count+1.
④p point to the next node, turn ②.
⑤ if Count equals K, the lookup succeeds, outputs the value of the data field of the node, returns 1; otherwise, the description K


The value exceeds the length of the linear table, the lookup fails, and returns 0. (Check the value of K)
The ⑥ algorithm ends.


3) algorithm implementation
typedef int ELEMTYPE; Type definition for linked list data
Structure definition of typedef struct lnode{//linked list nodes
Elemtype data; Node data
struct Lnode *link; Node link pointer
}lnode, *linklist; C knowledge, forget to go back to check


int Search_k (linklist list,int k) {      //Look up the list of the last K nodes of the linked list and output the value of the data field of the node      lnode *p = List->link, *q=list->link ;  P, q indicates the first node      int count=0;      while (p!=null) {  //traverse the list until the last node          if (count<k) count++;  Count, if count<k move only P          else q=q->link;          p-p->link;  Then let p, Q synchronously Move      }//while      if (count<k)          return 0;  Lookup failed to return 0      else{    //Otherwise print and return 1      printf ("%d", q->data);      return 1;      }  } Search_k  



Let's get here first. 2015-2-27 The brain melon seeds feel again to expand, there are 8 single-linked list of knowledge, to my good digestion, after the occasional share ~ welcome attention (*^__^*) garrylin~

Single linked list Knowledge Point Learning--android Foundation Series (I.)

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.