题解 | #划分链表#
划分链表
http://www.nowcoder.com/practice/1dc1036be38f45f19000e48abe00b12f
/**
- struct ListNode {
- int val;
- struct ListNode *next;
- };
- C语言声明定义全局变量请加上static,防止重复定义 / /*
- 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
- @param head ListNode类
- @param x int整型
- @return ListNode类 / struct ListNode partition(struct ListNode* head, int x ) { struct ListNode* pd=NULL; struct ListNode* ps=(struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* pn=(struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* pss=NULL; struct ListNode* pnn=NULL; pd=head; pnn=pn; pss=ps; if(head==NULL||head->next==NULL) { return head; } while(pd!=NULL) { if(pd->val<x) { pn->next=pd; pd=pd->next; pn=pn->next; } else { ps->next=pd; pd=pd->next; ps=ps->next; } } ps->next=NULL; pn->next=pss->next; pnn=pnn->next; return pnn; }//讨论区有思路