Odd Even Linked List

  • 1

    Click here to see the full article post

  • 0

    an easy understand idea,good

  • 0

    I am doing something similar albeit with more variables. My solution works for all inputs except 1->2->3 for which I am getting a run time error. Can someone please help me out why I am getting a run time error for an input of size 3?

      ListNode *even, *firsteven, *odd, *curr;
      if(head == NULL)
          return NULL;
      odd = head;
      even = head;
      curr = head;
      if(head->next) {
          even = even->next;
          firsteven = head->next;
      else return head;
          curr = head->next->next;
      else return head;
      while(curr) {
          even->next = curr->next;
          curr->next = NULL;
          odd->next = curr;
          curr->next = firsteven;
          odd = odd->next;
          even = even->next;
          even->next ? curr = even->next : curr = NULL;
      return head;

  • -1

    Wrong test cases.

  • -1

    Forgot to clarify. Here:


  • 0

    @piy9 It has been some time since you posted, and I don't know if you have found out the answer or not. But anyway, I will share my thoughts here.

    I got similar error in my first few submits: if the whole list has odd number nodes in total, like 3, 5, 7 and etc., I will get run time error.

    It turned out to be that I have to specifically make my even list's last node point to NULL. Otherwise it would generate a ring list. Try it. I guess it would solve your problem too.

  • 0

    This is what I tried, Iterate list with current node with a loop counter. if loop counter is even then add current node to evenlist otherwise add to oddlist.

    public ListNode OddEvenList(ListNode head)
    if(head == null || head.next == null)
    return head;

        ListNode currNode = head; 
        ListNode oddList  = currNode;
        ListNode evenList = currNode.next;
        ListNode evenHead = evenList;
        currNode = currNode.next.next;
        int lpCnt = 3;
        while(currNode != null)
            if(lpCnt % 2 == 0)
               evenList.next = currNode;
               evenList = evenList.next;
               oddList.next = currNode;
               oddList = oddList.next;
            currNode = currNode.next;
        if (evenList != null)
            evenList.next = null;
        if(oddList != null)
            oddList.next = evenHead;
        return head;   


  • 0

    In Ruby, here's what that looks like

    def odd_even_list(head)
      return head if head.nil? || head.next.nil? # We need at least 2 nodes
      odd_tail = head
      current = head.next
      while current && current.next do
        next_node = current.next
        current.next = next_node.next # skip over odd element
        even_head = odd_tail.next # beginning of even chain
        odd_tail.next = next_node # link to previous node
        next_node.next = even_head # link to next node
        odd_tail = next_node # update odd_tail
        current = current.next # move forward

  • 0
    This post is deleted!

Log in to reply

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