// 删除有序链表中重复的元素-I
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
struct ListNode* deleteDuplicates(struct ListNode* head ) {
// write code here
//空链表就直接返回空指针
if(!head) // 注:在条件判断中,head == NULL 和 !head 所起的作用是一样的。
return NULL;
struct ListNode* cur = head;
while(cur->next && cur) // 注意循环条件
{
struct ListNode* next = cur->next;
//若相等,则删掉下一个节点
if(cur->val == next->val)
{
cur->next = next->next;
free(next);
}
//若不等,则将下一个节点变为当前节点
else
cur = cur->next;
}
return head;
}
// 删除有序链表中重复的元素-II
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
struct ListNode* deleteDuplicates(struct ListNode* head ) {
// write code here
//空或只有一个节点
if(!head || !(head->next))
return head;
//设置哨兵位
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
dummy->next = head;
struct ListNode* pre = dummy; // 重复值节点区间的前驱节点
struct ListNode* cur = pre->next;
while(cur && cur->next) // cur->next:当下面的if语句中next是空指针时,避免空指针的解引用
{
struct ListNode* next = cur->next;
if(cur->val == next->val)
{
//跳过重复数字的节点,next是重复值节点区间的后一个节点
while(next && cur->val == next->val) // next:避免next最后是空指针,比如链表为[1,1]
next = next->next;
//连接
pre->next = next;
//删相同数字的节点
while(cur != next)
{
struct ListNode* tmp = cur->next;
free(cur);
cur = tmp;
}
}
else
{
pre = cur;
cur = cur->next;
}
}
head = dummy->next;
free(dummy);
dummy = NULL;
return head;
}
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
struct ListNode* sortInList(struct ListNode* head ) {
// write code here
struct ListNode *p=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode *q=(struct ListNode*)malloc(sizeof(struct ListNode));
int t;
for(p=head;p!=NULL;p=p->next)//选定的位置,从第一个位置开始
{
for(q=p->next;q!=NULL;q=q->next)//每次遍历一遍链表,只要比选定位置的数据小就交换
{
if(q->val<p->val)
{
t=q->val;
q->val=p->val;
p->val=t;
}
}
}
return head;
}
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param m int整型
* @param n int整型
* @return ListNode类
*/
struct ListNode* reverseBetween(struct ListNode* head, int m, int n ) {
// write code here
struct ListNode *pre = NULL;
struct ListNode *cur = head;
struct ListNode *next = NULL;
struct ListNode *left = NULL;
int i;
if(head)
{
next = cur->next;
}
if(m == 1)
{
left = cur;
for(i=0; i<n; i++)
{
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
head = pre;
left->next = next;
return head;
}
for(i=0; i<m-1; i++)
{
pre = cur;
cur = cur->next;
next = cur->next;
}
for(i=m-1; i<n-1; i++)
{
next = cur->next;
cur->next = next->next;
next->next = pre->next;
pre->next = next;
}
return head;
}
bool hasCycle(struct ListNode* head) {
struct ListNode* fast, * slow;
fast = slow = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;//如果快指针走两步以上,可能会错过重合点,导致效率低下,甚至可能永远不能相遇
if (fast == slow) {
return true;
}
}
return false;
}
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param pHead ListNode类
* @return ListNode类
*/
struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
struct ListNode *temp;
struct ListNode *preListNode = NULL;
struct ListNode *currentListNode = pHead;
if( (NULL == pHead) || (NULL == currentListNode))
return pHead;
while(currentListNode)
{
temp = currentListNode;
currentListNode = currentListNode->next;
temp->next = preListNode;
preListNode = temp;
}
return preListNode;
}