leetcode 面试题24. 反转链表--添加头结点--头插法(期间出现指针位置设计为题出现无限循环)+++IDE代码

//基础题目 头插法
//具体的是双指针加上一个头结点指针 将第二个元素 采用头插法以此向后进行
/根据测试用例报错 看出测试用例定义的是一个不带头结点的链表

//没有头结点那就添加一个头结点

主要花时间找的错误:通过printf发现 post 保存的地址信息与 头结点一致 出现无限循环

修改方法是将post向右边移动一位

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 //根据测试用例报错 看出测试用例定义的是一个不带头结点的链表
struct ListNode* reverseList(struct ListNode* head){
	//基础题目 头插法
	//具体的是双指针加上一个头结点指针 将第二个元素 采用头插法以此向后进行
    //没有头结点那就添加一个头结点
    struct ListNode *headadd;
    headadd =(struct ListNode *)malloc(sizeof(struct ListNode));
    headadd ->next = head;
	struct ListNode *pre;
	struct ListNode *post;
	pre  = head;
	post = head;
	if (head == NULL){
		return NULL;
	}
	if (pre){//修改避免无限循环
		pre = pre->next;
	}
	else{
		return head;
	}

	while (pre != NULL){//通过printf发现 post 保存的地址信息与 head一致 出现无限循环

		post->next = pre->next;
		pre->next = headadd->next;
		headadd->next = pre;
		
		pre = post->next;
	}
	return headadd->next;
}

IDE代码:

#include<stdio.h>
struct ListNode* partition(struct ListNode* head, int x);
struct ListNode* reverseList(struct ListNode* head);
/*
编写程序以 x 为基准分割链表,使得所有小于 x 的节点排在大于或等于 x 的
节点之前。如果链表中包含 x,x 只需出现在小于 x 的元素之前(如下所示)。
分割元素 x 只需处于“右半部分”即可,其不需要被置于左右两部分之间。
示例 :
输入 : head = 3->5->8->5->10->2->1, x = 5
输出 : 3->1->2->10->5->5->8

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

void main(){
	struct ListNode *head;
	head = (struct ListNode*)malloc(sizeof(struct ListNode));
	struct ListNode *node1;
	node1 = (struct ListNode*)malloc(sizeof(struct ListNode));
	int value = 1;
	node1->val = value;
	head->next = node1;
	node1->next = NULL;
	struct ListNode *node2;
	node2 = (struct ListNode*)malloc(sizeof(struct ListNode));
	value = 2;
	node2->val = value;
	node1->next = node2;
	node2->next = NULL;
	struct ListNode *node3;
	node3 = (struct ListNode*)malloc(sizeof(struct ListNode));
	value = 3;
	node3->val = value;
	node2->next = node3;
	node3->next = NULL;
	struct ListNode *node4;
	node4 = (struct ListNode*)malloc(sizeof(struct ListNode));
	value = 4;
	node4->val = value;
	node3->next = node4;
	node4->next = NULL;
	struct ListNode *first;
	struct ListNode *secode;
	first = secode = head;
/*	first = partition(first, 2);
	while (first->next != NULL){
		printf("%d", first->next->val);
		first = first->next;
	}*/
	head = reverseList(head->next);
	while (head->next != NULL){
		printf("%d", head->next->val);
		head = head->next;
		//printf("aaa");
	}
	system("pause");

}

//不需要保证分割节点不要保证左边小右边大
struct ListNode* partition(struct ListNode* head, int x){
	//根据传入的参数 节点加上 分割点
	struct  ListNode *pre;
	struct  ListNode *post;

	post = head;

	if (head == NULL){
		return head;
	}

	pre = post->next;
	int mark = 0;
	while (pre != NULL){
		if (pre->val >= x){
			mark = 1;
		}
		if (mark == 1 && pre->val < x){
			post->next = pre->next;
			pre->next = head->next;
			head->next = pre;
			pre = post->next;
		}
		else{
			pre = pre->next;
			post = post->next;
		}


		//	printf("%d", post->val);
	}
	return head;
}


struct ListNode* reverseList(struct ListNode* head){
	//基础题目 头插法
	//具体的是双指针加上一个头结点指针 将第二个元素 采用头插法以此向后进行
	struct ListNode *pre;
	struct ListNode *post;
	pre  = head;
	post = head;
	if (head == NULL){
		return NULL;
	}
	pre = pre->next;
	post = post->next;

	if (pre){//修改避免无限循环
		pre = pre->next;
	}
	else{
		return head;
	}

	while (pre != NULL){//通过printf发现 post 保存的地址信息与 head一致 出现无限循环

		post->next = pre->next;
		pre->next = head->next;
		head->next = pre;
		
		pre = post->next;
	}
	return head;
}
发布了212 篇原创文章 · 获赞 32 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_42664961/article/details/104321275