Two ways to find the penultimate node of the linked list

Source: Internet
Author: User

1#include <stdio.h>2#include <stdlib.h>3 /*4 title: Find the bottom-count K-nodes in a linked list, k>05 Idea 1: Consider the list of empty,k< linked list length,k> greater than the list length of three cases6 1. The list is empty, i.e.: there is no reciprocal K-node7 2.k>len. i.e.: there is no reciprocal k-node. 8 3.k<len. The second-to-last node is a positive len-1 (len-2+1) node, and the third-to-last node is a positive len-2 (len-3+1) node, then the reciprocal K-node is a positive number len-k+1 node.9 Idea 2: Define two pointers, point to the head node, the first pointer traverses to the K-node first, and the difference between them is K-1 a node. At this point two pointers go forward together, and when the first pointer points to the next node at the end of the chain,Ten The second pointer is pointing exactly to the bottom of the K-node. One  A example to find the bottom 4th node: Head Node 1 2 3 4 5 6 7 8 9 P2 point to the 4th node first - P1 (Difference 3 JD) P2 - at this time P1 P2, while moving forward, when the P2 point to the end of the next node, P1 just point to the bottom 4th number the Head Junction 1 2 3 4 5 6 7 8 9 - P1 (Difference 3 JD) P2 - */ -typedefstructnode + { -     intdata; +     structNode *Next; A }node; atNODE *createlist () - { -Node * head = (node *)malloc(sizeof(NODE)); -Head->next =NULL; -  -     returnhead; in } - voidInsertnode (NODE *head,intinsertdata) to { +Node * sur = (node *)malloc(sizeof(NODE)); -Sur->data =InsertData; the  *Sur->next = head->Next; $Head->next =sur;Panax Notoginseng  - } the voidTraverlist (NODE *head) + { A     inti =1; theHead = head->Next; +      while(head) -     { $printf"node%d =%d\n",i,head->data); $Head = head->Next; -i++; -     } the } - intLenlist (NODE *head)Wuyi { the     intLen =0; -Head = head->Next; Wu      while(head) -     { Aboutlen++; $Head = head->Next; -     } -     returnLen; - } A /* + //Idea one: the void Looknode (NODE *head,int len,int k) - { $ int i = 1; the if (head->next = = NULL | | k<=0) the printf ("The linked list is empty, or the node for the query does not exist.") There is no reciprocal%d node \ n ", k); the else if (K>len) the printf ("The node number of the query is greater than the linked list length, there is no node \ n"); - Else in     { the head = head->next; the While (head) About         { the if (i = = len-k+1) the             { the printf ("Last%d node data =%d\n", k,head->data); + Break ; -             } the i++;Bayi head = head->next; the         } the     }     - } - */ the //idea two: the voidLooknode (NODE *head,intLenintk) the { the     inti =K; -NODE * p1,*P2; theP1 = P2 =head; the     if(k>len| | k<=0) the     {94printf"The linked list is empty, or the node for the query does not exist. There is no reciprocal%d node \ n", k); the         return ; the     } the 98     //P2 points to the first K-node About      while(k>=1) -     {101P2 = p2->Next;102k--;103     }104     //P1,P2 at the same time, when P2 points to the next node of the tail node (P2 is null at this point), P1 points to the reciprocal K-node . the      while(p2)106     {107P1 = p1->Next;108P2 = p2->Next;109     } theprintf"count%d node data =%d\n",i,p1->data);111      the     return ;113 } the intMainvoid) the { theNODE * head =createlist ();117      for(inti =0;i< -; i++)118Insertnode (Head,rand ()% -);119 Traverlist (head); -     intLen =Lenlist (head);121     intK;122printf"Please enter the node you want to find \ n");123scanf"%d",&k);124 Looknode (head,len,k); the 126     return 0;127}

Two ways to find the penultimate node of the linked list

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.