Why this line leads to runtime error.....(c++)


  • -1
    O

    /**

    • Definition for singly-linked list.
    • struct ListNode {
    • int val;
      
    • ListNode *next;
      
    • ListNode(int x) : val(x), next(NULL) {}
      
    • };
      /
      class Solution {
      public:
      ListNode
      removeElements(ListNode* head, int val)
      {
      ListNode *prehead;
      //prehead->val = 0; //if has,runtime error,why?
      prehead->next = head;
      ListNode *pre = prehead;
      ListNode *cur = head;
      while(cur)
      {
      if(cur->val == val)
      {
      pre->next = cur->next;
      cur = cur->next;
      }
      else
      {
      pre = cur;
      cur = cur->next;
      }
      }
      return prehead->next;
      }
      };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.