• 3.4 从无头单链表中删除节点


    假设一个没有头指针的单链表,只有一个指向中间节点(不是头尾节点)的指针,请将该节点从单链表中删除。

    思路:假设中间一段链表为 A->B->C,而p指向节点B。因为没有头节点,其实无法追溯回A,然后使A->C。所以这里的技巧是将C中的value值赋给B中,使B成为C,然后删除真正的C,从而达到目的。

     1 #include "stdafx.h"
     2 #include <iostream>
     3 
     4 using namespace std;
     5 
     6 const int MID_NUM = 3;
     7 const int MAX_NUM = 8;
     8 
     9 struct node{
    10     int value;
    11     struct node *next;
    12 };
    13 
    14 node* createMidLinkedList(int midNum,node *headPtr){
    15     node *midPtr,*p;
    16 
    17     for(int i=0;i<MAX_NUM;i++){
    18         p = (node *)malloc(sizeof(node));
    19         p->value = i;
    20         p->next = headPtr->next;
    21         headPtr->next = p;
    22         if(p->value==midNum)
    23             midPtr = p;
    24         cout<<p->value<<" -> ";
    25     }
    26     cout<<endl<<endl;
    27     return midPtr;
    28 }
    29 
    30 //delete middle node
    31 bool deleteMidNode(node *midPtr){
    32     if(midPtr==NULL || midPtr->next==NULL) return false;
    33 
    34     node *mid,*midNext;
    35     mid = midPtr;
    36     midNext = mid->next;
    37 
    38     if(midNext!=NULL){
    39         mid->value = midNext->value;
    40         mid->next = midNext->next;
    41         delete midNext; //
    42     }
    43     
    44 
    45     return true;
    46 }
    47 
    48 int _tmain(int argc, _TCHAR* argv[])
    49 {
    50     node *head= (node *)malloc(sizeof(node));; //不可在createMidLinkedList方法中分配内存
    51     head->next = NULL; //一定不可以漏,否则判断head->next==NULL时会返回false!
    52     node *mid = createMidLinkedList(MID_NUM,head);
    53     if(deleteMidNode(mid)){
    54         head = head->next;
    55         while(head!=NULL){
    56             cout<<head->value<<" -> ";
    57 
    58             head = head->next;
    59         }
    60     }else{
    61         cout<<"Failed to delete!"<<endl;
    62     }
    63 }

    输出为:

    可见节点3(MID_NUM)已经被删除。

    引申问题:只有一个头节点,翻转单链表,要求只遍历一次

    利用构造链表时的方法,修改代码如下:

     1 node *reverseLinkedList(node *headPtr){
     2     if(headPtr==NULL) return NULL;
     3 
     4     node *head,*newHead,*p,*temp;
     5     head=headPtr;
     6     p = head->next;
     7     newHead = (node *)malloc(sizeof(node));
     8     newHead->next = NULL;
     9 
    10     while(p!=NULL){
    11         temp = p->next;
    12         p->next    = newHead->next;
    13         newHead->next = p;
    14         p = temp;
    15     }
    16 
    17     return newHead;
    18 }
    19 
    20 int _tmain(int argc, _TCHAR* argv[])
    21 {
    22     node *head= (node *)malloc(sizeof(node));; //不可在createMidLinkedList方法中分配内存
    23     head->next = NULL; //一定不可以漏,否则判断head->next==NULL时会返回false!
    24     node *mid = createMidLinkedList(MID_NUM,head);
    25     node *hPtr; //new header pointer
    26     if(deleteMidNode(mid)){
    27         hPtr = head;
    28 
    29         //print list
    30         head = head->next;
    31         while(head!=NULL){
    32             cout<<head->value<<" -> ";
    33             head = head->next;
    34         }
    35         cout<<endl<<endl;
    36 
    37         head = reverseLinkedList(hPtr);
    38 
    39         //print list
    40         head = head->next;
    41         while(head!=NULL){
    42             cout<<head->value<<" -> ";
    43             head = head->next;
    44         }
    45 
    46     }else{
    47         cout<<"Failed to delete!"<<endl;
    48     }
    49 }

    可见输出如下:

  • 相关阅读:
    gflag使用
    INTERVIEW #2
    Decision Tree
    Java FAQ
    K-Nearest Neighbors
    INTERVIEW #1
    C++ FAQ
    INTERVIEW #0
    Selection Sort
    TCP 3-Way Handshake
  • 原文地址:https://www.cnblogs.com/techyc/p/2695040.html
Copyright © 2020-2023  润新知