-
Notifications
You must be signed in to change notification settings - Fork 0
/
143_reorder_list.c
90 lines (86 loc) · 1.91 KB
/
143_reorder_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Copyright (C) 2016 Leonard Ding <dingxiaoyun88@gmail.com> */
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* reverseLinkedList(struct ListNode* head) {
if(head == NULL){
return NULL;
}
struct ListNode* current;
struct ListNode* node;
struct ListNode* nextNode;
current = head;
node = current->next;
while(node){
nextNode = node->next;
node->next = current;
current = node;
node = nextNode;
}
head->next = NULL;
return current;
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
void reorderList(struct ListNode* head) {
if(head == NULL){
return;
}
struct ListNode* reverseHead = head;
struct ListNode* node = head;
int step = 0;
while(node){
node = node->next;
step++;
}
if(step == 1){
return;
}
node = head;
int i;
for(i = 0; i < step / 2; i++){
node = node->next;
}
reverseHead = reverseLinkedList(node);
node = head;
struct ListNode* tmp;
struct ListNode* node2 = reverseHead;
struct ListNode* tmp2;
for(i = 0; i < step / 2; i++){
tmp = node->next;
tmp2 = node2->next;
node->next = node2;
node2->next = tmp;
node = tmp;
node2 = tmp2;
}
if(step % 2 == 1){
node->next = tmp2;
node = node->next;
}
node->next = NULL;
}
int main(){
struct ListNode* head = malloc(sizeof(struct ListNode*));
struct ListNode* n1 = malloc(sizeof(struct ListNode*));
struct ListNode* n2 = malloc(sizeof(struct ListNode*));
head->val = 1;
n1->val = 2;
n2->val = 3;
head->next = n1;
n1->next = n2;
n2->next = NULL;
reorderList(head);
return 0;
}