马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Code:
#include <stdio.h>
struct Node{
int val;
struct Node *next;
};
struct Node * reverseList1(struct Node *hd, int n, int m){
int change_len = m - n + 1;
struct Node * result = hd;
struct Node * pre_head = NULL;
while(hd && --n){
pre_head = hd;
hd = hd->next;
}
struct Node * modify_node_tail = hd;
struct Node * newHead = NULL;
while(hd && change_len){
struct Node *next = hd->next;
hd->next = newHead;
newHead = hd;
hd = next;
change_len = change_len -1;
}
modify_node_tail->next = hd;
if(pre_head){
pre_head -> next = newHead;
}
else{
result = newHead;
}
return result;
}
int main(void){
struct Node a;
struct Node b;
struct Node c;
struct Node d;
struct Node e;
a.val = 10;
b.val = 20;
c.val = 30;
d.val = 40;
e.val = 50;
a.next = &b;
b.next = &c;
c.next = &d;
d.next = &e;
e.next = NULL;
struct Node * head = &a;
while(head){
printf("%d\n", head->val);
head = head->next;
}
printf("----------\n");
struct Node * head_2 = &a;
int x = 2;
int y = 4;
struct Node *ptr_1 = reverseList1(head_2, x, y);
while(ptr_1){
printf("%d\n", ptr_1->val);
ptr_1 = ptr_1->next;
}
printf("----------\n");
return 0;
}
Output:
10
20
30
40
50
----------
10
40
30
20
50
----------
|