206. 反转链表
#include<stdio.h>
#include<stdlib.h>
/**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
struct ListNode *next;
};
void printList(struct ListNode *head) {
if (head == NULL)
return;
struct ListNode *p = head;
while (p != NULL) {
printf("%d\t", p->val);
p = p->next;
}
printf("\n");
}
struct ListNode *initList(const int *intArray, const int n) {
struct ListNode *head = malloc(sizeof(struct ListNode));
head->val = intArray[0];
// head->next = NULL;
struct ListNode *p = head;
for (int i = 1; i < n; i++) {
struct ListNode *t = malloc(sizeof(struct ListNode));
t->val = intArray[i];
t->next = NULL;
p->next = t;
p = t;
}
return head;
}
struct ListNode *reverseList(struct ListNode *head) {
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode *r = head;
struct ListNode *p = head->next;
while (p != NULL) {
struct ListNode *t = p;
p = p->next;
t->next = r;
// r->next = NULL;
r = t;
}
head->next = NULL;
return r;
}
void freeList(struct ListNode *head) {
struct ListNode *p = head;
while (p != NULL) {
struct ListNode *t = p;
p = p->next;
free(t);
}
}
int main() {
const int arr[] = {1, 2, 3, 4, 5, 6};
struct ListNode *head = initList(arr, 6);
printList(head);
struct ListNode *res = swapPairs(head);
printList(res);
freeList(res);
return 0;
}
课程中给的示例
struct ListNode *reverseList(struct ListNode *head) {
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode *cur = head;
struct ListNode *prev = NULL;
while (cur != NULL) {
struct ListNode *temp = cur->next;
cur->next = prev;
prev = cur;
cur = temp;
}
return prev;
}
24. 两两交换链表中的节点
struct ListNode *swapPairs(struct ListNode *head) {
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode *r = head->next;
struct ListNode *p = head;
while (p != NULL) {
struct ListNode *t = p->next;
if (t == NULL) {
break;
}
p = t->next;
t->next = head;
if (p==NULL || p->next == NULL) {
head->next = p;
} else {
head->next = p->next;
}
head = p;
}
return r;
}
课程中给的示例
struct ListNode *swapPairs(struct ListNode *head) {
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode *sf = malloc(sizeof(struct ListNode));
struct ListNode *pre = sf;
pre->next = head;
while (pre->next != NULL && pre->next->next != NULL) {
struct ListNode *a = pre->next;
struct ListNode *b = a->next;
struct ListNode *tmp = b->next;
pre->next = b;
b->next = a;
a->next = tmp;
pre = a;
}
head = sf->next;
free(sf);
return head;
}
141. 环形链表
#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
#include "uthash.h"
/**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode *initList(const int *intArray, const int n, int pos) {
struct ListNode *arr[n];
struct ListNode *head = malloc(sizeof(struct ListNode));
head->val = intArray[0];
head->next = NULL;
arr[0] = head;
struct ListNode *p = head;
for (int i = 1; i < n; i++) {
struct ListNode *t = malloc(sizeof(struct ListNode));
t->val = intArray[i];
t->next = NULL;
arr[i] = t;
p->next = t;
p = t;
}
if (pos > -1 && pos < n) {
struct ListNode *c = arr[pos];
p->next = c;
}
return head;
}
bool hasCycle(struct ListNode *head) {
if (head == NULL || head->next == NULL) {
return false;
}
struct ListNode *p = head;
struct ListNode *p2 = head;
while (p != NULL) {
if (p->next == NULL) { break; }
p = p->next;
if (p2->next == NULL || p2->next->next == NULL) { break; }
p2 = p2->next->next;
if (p == p2) { return true; }
}
return false;
}
//struct hashTable {
// struct ListNode* key;
// UT_hash_handle hh;
//};
//
//struct hashTable* hashtable;
//
//struct hashTable* find(struct ListNode* ikey) {
// struct hashTable* tmp;
// HASH_FIND_PTR(hashtable, &ikey, tmp);
// return tmp;
//}
//
//void insert(struct ListNode* ikey) {
// struct hashTable* tmp = malloc(sizeof(struct hashTable));
// tmp->key = ikey;
// HASH_ADD_PTR(hashtable, key, tmp);
//}
//
//bool hasCycle(struct ListNode* head) {
// hashtable = NULL;
// while (head != NULL) {
// if (find(head) != NULL) {
// return true;
// }
// insert(head);
// head = head->next;
// }
// return false;
//}
int main() {
const int arr[] = {3, 2, 0, -4};
struct ListNode *head = initList(arr, 4, -1);
bool res = hasCycle(head);
printf("%d", res);
return 0;
}
课程中给的示例
bool hasCycle(struct ListNode *head) {
if (head == NULL || head->next == NULL) {
return false;
}
struct ListNode *fast = head;
struct ListNode *slow = head;
while (fast != NULL && slow != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}