# 链表反转+交叉链接

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <stack>

using namespace std;

struct ListNode
{
int val;
ListNode* next;
ListNode(int x) :val(x),next(NULL) {};
};

ListNode* reverse(ListNode* root)
{
ListNode* new_head = root, * node = root->next;
while (node)
{

root->next = node->next;
node = root->next;
}
}
/*

*/
ListNode* changeList(ListNode* root)
{
int len = 0;
ListNode* node = root, * past_head = NULL, * new_head = root, * p = root, * q, * p_next, * q_next;
while (node)
{
len++;
node = node->next;
}
node = root;
int cnt=0;
while (cnt<len/2-1)
{
node = node->next;
cnt++;
}

node->next=NULL;  // 这儿要特别注意，一定要将前半段链表末位指向NULL，不然会死循环

cnt = 0;
while (q)
{
p_next = p->next;
q_next = q->next;
p->next = q;
q->next = p_next;
p = p_next;
q = q_next;
}

}

int main()
{
ListNode* p1 = new ListNode(3);
ListNode* p2 = new ListNode(2);
ListNode* p3 = new ListNode(1);
ListNode* p4 = new ListNode(4);

p1->next = p2;
p2->next = p3;
p3->next = p4;

ListNode* res = changeList(p1);

while (res)
{
cout << res->val<<" ";
res = res->next;
}

system("pause");
}