C实现 LeetCode->Partition List(双指针大法)(单链表)


/**

 *  Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

 You should preserve the original relative order of the nodes in each of the two partitions.

 

 

 For example:

 

 Given 1->4->3->2->5->2 and x = 3,

 

 return 1->2->2->4->3->5.


 题意:给定一个单链表和一个x,把链表中小于x的放到前面,大于等于x的放到后面,每部分元素的原始相对位置不变。

 

 思路:其实很简单,遍历一遍链表,把小于x的都挂到head1后,把大于等于x的都放到 (筛选出来的小于等于 x的最后一个节点)后,最后再把大于等于的链表挂到小于链表的后面就可以了。

 


 */




//
//  PartitionList.c
//  Algorithms
//
//  Created by TTc on 15/6/22.
//  Copyright (c) 2015年 TTc. All rights reserved.
//
/**
 *  Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
 You should preserve the original relative order of the nodes in each of the two partitions.
 
 
 For example:
 
 Given 1->4->3->2->5->2 and x = 3,
 
 return 1->2->2->4->3->5.

 题意:给定一个单链表和一个x,把链表中小于x的放到前面,大于等于x的放到后面,每部分元素的原始相对位置不变。
 
 思路:其实很简单,遍历一遍链表,把小于x的都挂到head1后,把大于等于x的都放到 (筛选出来的小于等于 x的最后一个节点)后,最后再把大于等于的链表挂到小于链表的后面就可以了。
 

 */
#include "PartitionList.h"
#include <stdbool.h>
#include <stdlib.h>
#include "List.h"


/********************************************************************/
// LeetCode
/********************************************************************/

struct ListNode {
    int val;
    struct ListNode *next;
};


struct ListNode*
partition(struct ListNode* head, int x) {
    struct ListNode *left = (struct ListNode *)malloc(sizeof(left)); //保存小于x的节点     的第一个 节点
    struct ListNode *right = (struct ListNode *)malloc(sizeof(right));//保存大于等于x的节点  的第一个 节点
    
    struct ListNode *left_cursor = left;  //遍历左部分的 临时指针
    struct ListNode *right_cursor = right;//遍历右部分的 临时指针
    
    while(head){
        if(head->val < x){
            left_cursor->next = head;
            left_cursor = left_cursor->next;
        }else{
            right_cursor->next = head;
            right_cursor = right_cursor->next;
        }
        head = head->next;
    }
    
    right_cursor->next = NULL;
    left_cursor->next = right->next;
    
    return left->next;
}
/********************************************************************/
/********************************************************************/



/********************************************************************/
// List.c 是范性类 单链表
/********************************************************************/

ListElmt*
tt_partition(ListElmt* head, int x) {
    
    ListElmt *left = (ListElmt *)malloc(sizeof(left)); //保存小于x的节点     的第一个 节点
    ListElmt *right = (ListElmt *)malloc(sizeof(right));//保存大于等于x的节点  的第一个 节点
    
    ListElmt *left_cursor = left;  //遍历左部分的 临时指针
    ListElmt *right_cursor = right;//遍历右部分的 临时指针

    ListElmt *curr = head;
    while(curr){
        //筛选出来 小于 x的节点
        if(*(int *)(curr->data) < x){
            left_cursor->next = curr;
//            printf("left_cursor->data======>%d\n", *(int*)(left_cursor->next->data));
            left_cursor = left_cursor->next;
        }else{//筛选出来 大于等于 x的节点  按原来的次序 挂载
            right_cursor->next = curr;
            right_cursor = right_cursor->next;
        }
        curr = curr->next;
    }
    
    right_cursor->next = NULL;
    left_cursor->next = right->next;

    return left->next;
    
}

ListElmt*
tt_partition02(ListElmt* head, int x) {

    ListElmt left;  //保存小于x的节点     的第一个 节点
    ListElmt right; //保存大于等于x的节点  的第一个 节点
    ListElmt *left_cursor;//保存  小于x的 节点
    ListElmt *right_cursor;//保存 大于等于x的 节点
    left_cursor = &left;
    right_cursor = &right;
    ListElmt *curr = head;
    while(curr){
        //筛选出来 小于 x的节点
        if(*(int *)(curr->data) < x){
            left_cursor->next = curr;
            //            printf("left_cursor->data======>%d\n", *(int*)(left_cursor->next->data));
            left_cursor = left_cursor->next;
        }else{//筛选出来 大于等于 x的节点  按原来的次序 挂载
            right_cursor->next = curr;
            right_cursor = right_cursor->next;
        }
        curr = curr->next;
    }
    
    right_cursor->next = NULL;
    left_cursor->next = right.next;
    
    return left.next;
}

void
test_tt_partition(){
    List l1;
    list_init(&l1, free);
    int *data ;
    int array[10] = {2,5,2,3,4,1};
    for (int i = 0; i< 6; i++) {
        if ((data = (int *)malloc(sizeof(int))) == NULL)
            return ;
        *data = array[i];
        if (list_ins_next(&l1, NULL, data) != 0)  //逐个插入元素
            return;
    }
    print_list(&l1);
    ListElmt* result = tt_partition(list_head(&l1),2);
    printf("反转后*****************\n");
    print_listNode(result);

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值