LeetCode -- Odd Even Linked List C语言 AC Code

Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.

The first node is considered odd, and the second node is even, and so on.

Note that the relative order inside both the even and odd groups should remain as it was in the input.

Example 1:

image

Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]

Example 2:

image

Input: head = [2,1,3,5,6,4,7]
Output: [2,3,6,7,1,5,4]

Constraints:

  • The number of nodes in the linked list is in the range [0, 104].
  • -106 <= Node.val <= 106

Follow up: Could you solve it in O(1) space complexity and O(nodes) time complexity?

我的方法的主要思路是, 设定两个奇偶链表头, 然后分别使用游标来标记两个链表的当前节点. 然后遍历链表将遍历到的结点分配到所属的链表之中.

当然这里面的代码冗余量感觉比较高, 之前有干过有种设计模式是能够解决这样的问题的, 具体的给忘记了.

看了看提交记录, 耗时最短的代码感觉还是很简洁的, 这里要贴一贴.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

typedef struct ListNode ListNode;

struct ListNode* oddEvenList(struct ListNode* head) {
    ListNode* oddHead, * evenHead, * tmp, * oddPointer, * evenPointer;
    int positionCount = 1;
    tmp = head;
    oddHead = oddPointer = evenHead = evenPointer = NULL;

    while (tmp != NULL) {
        if (positionCount % 2 == 0) {
            if (evenHead == NULL) {
                evenHead = evenPointer = tmp;
            }
            else {
                evenPointer->next = tmp;
                evenPointer = tmp;
            }
        }
        else {
            if (oddHead == NULL) {
                oddHead = oddPointer = tmp;
            }
            else {
                oddPointer->next = tmp;
                oddPointer = tmp;
            }
        }
        tmp = tmp->next;
        positionCount++;
    }
    if (oddPointer && evenPointer) {
        oddPointer->next = evenHead;
        evenPointer->next = NULL;
    }

    return head;

}

在 while 中定义变量,这么做虽然没有一开始在程序头部定义变量显得清晰,但是这么做能够根据变量的作用域 在while 循环结束后自动释放定义的变量内容, 减少内存的消耗.

程序的运行大概是这么个意思

大概是这么个意思

主要是通过快指针快速前进 作为 奇数节点赋值给慢指针, 然后使用fastpre 来指向 快指针的下一个节点作为偶数节点.

后面整合成一条链表的情况似乎没看太明白.... 就缕顺成一条链表啦?

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* oddEvenList(struct ListNode* head){
    
    
    if  ( (head == NULL) || (head->next == NULL)) return head;
    
    struct ListNode* slow = head;
    struct ListNode* fast = head->next->next;
    //fastpre - is the just one node previous to fast
    struct ListNode* fastpre = head->next;
        
        
    while(slow && fast) {
        //copy current 'fast' in order to move it next to slow
        struct ListNode* temp = fast;
        //unlink current fast
        fastpre->next = fast->next;
        
        //move fast  two positions ahead
        fast = fast->next ? fast->next->next : NULL;
        //fast pre already moved by one postion, move by one more position
        if (fast != NULL) fastpre = fastpre->next;
        
        //now insert copied fast next to slow
        struct ListNode* slownext = slow->next;
        slow->next = temp;
        temp->next = slownext;
        slow = slow->next;
    }

     return head;

}

你可能感兴趣的:(LeetCode -- Odd Even Linked List C语言 AC Code)