Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ swap nodes in a linked list

void swapNodeValues(int node1, int node2) {
  
  //1. count the number of nodes in the list
  Node* temp = head;
  int N = 0;
  while(temp != NULL) {
    N++;
    temp = temp->next;
  }

  //2. check if the swap positions are valid entries
  if(node1 < 1 || node1 > N || node2 < 1 || node2 > N)
    return;

  //3. traverse to the nodes where values to be swapped
  Node* pos1 = head;
  Node* pos2 = head;
  for(int i = 1; i < node1; i++) {
    pos1 = pos1->next;
  }
  for(int i = 1; i < node2; i++) {
    pos2 = pos2->next;
  }

  //4. swap the values of two nodes
  int val = pos1->data;
  pos1->data = pos2->data;
  pos2->data = val;
}
Source by www.alphacodingskills.com #
 
PREVIOUS NEXT
Tagged: #swap #nodes #linked #list
ADD COMMENT
Topic
Name
8+2 =