Advertisement
Guest User

Linked List - Removing Nodes

a guest
Jan 19th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1.  public static void RemoveSeq(Node<int> p) //Removes recurring digits in a sequence - 8,4,4,4,4,2 -> 8,4,2
  2.         {
  3.             Node<int> t = p, temp;
  4.             while (t.GetNext() != null)
  5.             {
  6.                 temp = t.GetNext();
  7.                 if(t.GetValue() == temp.GetValue())
  8.                 {
  9.                     t.SetNext(temp.GetNext());
  10.                     temp.SetNext(null);
  11.                 }
  12.                 else
  13.                 {
  14.                     t = t.GetNext();
  15.                 }
  16.             }
  17.         }
  18.        
  19.         public static Node<int> RemoveNum(Node<int> p, int n) //Removes all occurrences of digit "n" in list, returning the first node of the new list
  20.         {
  21.             Node<int> t, temp;
  22.             while (p != null && p.GetValue() == n) //Remove all starting nodes of value "n"
  23.             {
  24.                 t = p.GetNext();
  25.                 p.SetNext(null);
  26.                 p = t;
  27.             }
  28.             t = p; temp = p.GetNext();
  29.             while (temp != null) //Removes all other nodes of value "n"
  30.             {
  31.                 if(temp.GetValue() != n)
  32.                 {
  33.                     t = temp;
  34.                     temp = temp.GetNext();
  35.                 }                
  36.                 else
  37.                 {
  38.                     t.SetNext(temp.GetNext());
  39.                     temp = t.GetNext();
  40.                 }
  41.             }
  42.             return p;
  43.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement