My C code. Not concise but easily read.


  • 0
    S

    /**

    • Definition for singly-linked list.

    • struct ListNode {

    • int val;
      
    • struct ListNode *next;
      
    • };
      /
      struct ListNode
      oddEvenList(struct ListNode* head)
      {
      struct ListNode* oddList = NULL;
      struct ListNode* evenList = NULL;
      struct ListNode* currentNode = head;
      struct ListNode* preNode = NULL;
      int index = 1;

      while(currentNode != NULL)
      {
      if((index%2) != 0)
      {
      oddList = addToRearlist(currentNode->data, oddList);
      }
      else
      {
      evenList = addToRearlist(currentNode->data, evenList);
      }
      currentNode = currentNode->next;
      index++;

      }

      currentNode = oddList;
      preNode = oddList;

      while(currentNode != NULL)
      {
      preNode = currentNode;
      currentNode = currentNode->next;
      }

      preNode->next = evenList;

      return oddList;
      }

    struct ListNode* addToTailofList(int value, struct ListNode* list)
    {

    struct ListNode* tmpNode = NULL;
    struct ListNode* currentNode = NULL;
    struct ListNode* preNode = NULL;
    
    tmpNode = (struct ListNode*)malloc(sizeof(struct ListNode));
    tmpNode->data = value;
    
    if(list == NULL)
    {
        list = tmpNode;
    }
    else
    {
        currentNode = list;
        preNode = list;
        
        while(currentNode != NULL)
        {
            preNode = currentNode;
            currentNode = currentNode->next;
        }
        
        preNode->next = tmpNode;
    }
    
    return list;
    

    }


Log in to reply
 

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