zoukankan      html  css  js  c++  java
  • [刷题] 203 Remove Linked List Elements

    要求

    • 在链表中删除值为val的所有节点

    示例

    • 如 1->2->3->4->5->6->NULL,要求删除值为6的节点
    • 返回1->2->3->4->5->NULL

    思路

    • 删除一般元素(包括最后一个元素)
    • 删除第一个元素

    实现

    • 常规思路
     1 #include <iostream>
     2 using namespace std;
     3 
     4 struct ListNode {
     5     int val;
     6     ListNode *next;
     7     ListNode(int x) : val(x), next(NULL) {}
     8 };
     9 
    10 ListNode* createLinkedList(int arr[], int n){
    11     if( n == 0 )
    12         return NULL;
    13     ListNode* head = new ListNode(arr[0]);
    14     ListNode* curNode = head;
    15     for( int i = 1 ; i < n ; i ++ ){
    16         curNode->next = new ListNode(arr[i]);
    17         curNode = curNode->next;
    18     }
    19     return head;
    20 }
    21 
    22 void printLinkedList(ListNode* head){
    23     ListNode* curNode = head;
    24     while( curNode != NULL ){
    25         cout << curNode->val << " -> ";
    26         curNode = curNode->next;
    27     }
    28     cout<<"NULL"<<endl;
    29     return;
    30 }
    31 
    32 void deleteLinkedList(ListNode* head){
    33     ListNode* curNode = head;
    34     while( curNode != NULL){
    35         ListNode* delNode = curNode;
    36         curNode = curNode->next;
    37         delete delNode;
    38     }
    39     return;
    40 }
    41 
    42 class Solution {
    43 public:
    44     ListNode* removeElements(ListNode* head, int val) {
    45         
    46         while( head != NULL && head->val == val ){
    47             ListNode* delNode = head;
    48             head = delNode->next;
    49             delete delNode;
    50         }
    51         
    52         if( head == NULL )
    53             return NULL;
    54         
    55         ListNode* cur = head;
    56         while( cur->next != NULL ){
    57             
    58             if( cur->next->val == val ){
    59                 ListNode* delNode = cur->next;
    60                 cur->next = delNode->next;
    61                 delete delNode;
    62             }else
    63                 cur = cur->next;
    64         }
    65         return head;
    66     }
    67 };
    68 
    69 int main(){
    70     int arr[] = {1,2,3,4,5};
    71     int n = sizeof(arr)/sizeof(int);
    72     
    73     ListNode* head = createLinkedList(arr,n);
    74     Solution().removeElements(head,3);
    75     printLinkedList(head);
    76     
    77     deleteLinkedList(head);
    78     return 0;
    79 }
    View Code
    • 设置虚拟头节点

     1 #include <iostream>
     2 using namespace std;
     3 
     4 struct ListNode {
     5     int val;
     6     ListNode *next;
     7     ListNode(int x) : val(x), next(NULL) {}
     8 };
     9 
    10 ListNode* createLinkedList(int arr[], int n){
    11     if( n == 0 )
    12         return NULL;
    13     ListNode* head = new ListNode(arr[0]);
    14     ListNode* curNode = head;
    15     for( int i = 1 ; i < n ; i ++ ){
    16         curNode->next = new ListNode(arr[i]);
    17         curNode = curNode->next;
    18     }
    19     return head;
    20 }
    21 
    22 void printLinkedList(ListNode* head){
    23     ListNode* curNode = head;
    24     while( curNode != NULL ){
    25         cout << curNode->val << " -> ";
    26         curNode = curNode->next;
    27     }
    28     cout<<"NULL"<<endl;
    29     return;
    30 }
    31 
    32 void deleteLinkedList(ListNode* head){
    33     ListNode* curNode = head;
    34     while( curNode != NULL){
    35         ListNode* delNode = curNode;
    36         curNode = curNode->next;
    37         delete delNode;
    38     }
    39     return;
    40 }
    41 
    42 class Solution {
    43 public:
    44     ListNode* removeElements(ListNode* head, int val) {
    45         
    46         ListNode* dummyHead = new ListNode(0);
    47         dummyHead->next = head;
    48         
    49         ListNode* cur = dummyHead;
    50         while( cur->next != NULL ){
    51             
    52             if( cur->next->val == val ){
    53                 ListNode* delNode = cur->next;
    54                 cur->next = delNode->next;
    55                 delete delNode;
    56             }else
    57                 cur = cur->next;
    58         }
    59         
    60         ListNode* retNode = dummyHead->next;
    61         delete dummyHead;
    62         
    63         return retNode;
    64     }
    65 };
    66 
    67 int main(){
    68     int arr[] = {1,2,3,4,5};
    69     int n = sizeof(arr)/sizeof(int);
    70     
    71     ListNode* head = createLinkedList(arr,n);
    72     Solution().removeElements(head,3);
    73     printLinkedList(head);
    74     
    75     deleteLinkedList(head);
    76     return 0;
    77 }
    View Code

    相关

    • 82 Remove Duplicates from Sorted List II
    • 21 Merge Two Sorted Lists
  • 相关阅读:
    How to function call using 'this' inside forEach loop
    jquery.validate.unobtrusive not working with dynamic injected elements
    Difference between jQuery.extend and jQuery.fn.extend?
    Methods, Computed, and Watchers in Vue.js
    Caution using watchers for objects in Vue
    How to Watch Deep Data Structures in Vue (Arrays and Objects)
    Page: DOMContentLoaded, load, beforeunload, unload
    linux bridge
    linux bridge
    EVE-NG网卡桥接
  • 原文地址:https://www.cnblogs.com/cxc1357/p/12635764.html
Copyright © 2011-2022 走看看