0%

代码随想录-链表

203. 移除链表元素 easy

简单题,看看CPP的指针的写法就好

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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/

// 迭代法
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode * h=new ListNode();
h->next = head;
ListNode *t = h;
while(t->next != nullptr){
if(t->next->val == val){
ListNode * temp = t->next;
t->next = t->next->next;
delete temp;
}else
t = t->next;
}
return h->next;
}
};

// 递归法
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if (head == nullptr) {
return head;
}
head->next = removeElements(head->next, val);
return head->val == val ? head->next : head;
}
};

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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/

// 迭代法
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head == null){
return head;
}
ListNode hh = new ListNode();
ListNode h = hh;

h.next = head;
while(h.next != null){
if(h.next.val == val){
h.next = h.next.next;
}else{
h = h.next;
}
}
return hh.next;
}
}
// 递归法
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head == null){
return head;
}
head.next = removeElements(head.next, val);
return head.val == val? head.next:head;
}
}

707.设计链表 middle

考察基础,并不难,主要理解在cpp在class中创建struct或者java在class中创建class。

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
class MyLinkedList {
public:
struct LinkedNode {
int val;
LinkedNode * next;
LinkedNode () : val(0), next(nullptr) {}
LinkedNode (int x) : val(x), next(nullptr) {}
LinkedNode (int x, LinkedNode *next) : val(x), next(next) {}
};

MyLinkedList() {
preHead = new LinkedNode ();
_size = 0;
}

int get(int index) {
if (index > (_size - 1) || index < 0) {
return -1;
}
LinkedNode * t = preHead->next;
while(index--){
t = t->next;
}
return t->val;
}

void addAtHead(int val) {
LinkedNode * ins = new LinkedNode(val,preHead->next);
preHead->next = ins;
_size++;
}

void addAtTail(int val) {
LinkedNode * ins = new LinkedNode(val,nullptr);
LinkedNode* cur = preHead;
while(cur->next != nullptr){
cur = cur->next;
}
cur->next = ins;
_size++;
}

void addAtIndex(int index, int val) {
if(index > _size) return;
if(index < 0) index = 0;
LinkedNode * ins = new LinkedNode(val);
LinkedNode * t = preHead;
while(index--){
t = t->next;
}
ins->next = t->next;
t->next = ins;
_size++;
return;
}

void deleteAtIndex(int index) {
if(index < 0 || index >= _size) return;
LinkedNode * t = preHead;
while(index--){
t = t->next;
}
LinkedNode* tmp = t->next;
t->next = t->next->next;
_size--;
delete tmp;
return;
}

private:
int _size;
LinkedNode* preHead;

};

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
91
92
//单链表
class MyLinkedList {

class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val=val;
}
}
//size存储链表元素的个数
private int size;
//注意这里记录的是虚拟头结点
private ListNode head;

//初始化链表
public MyLinkedList() {
this.size = 0;
this.head = new ListNode(0);
}

//获取第index个节点的数值,注意index是从0开始的,第0个节点就是虚拟头结点
public int get(int index) {
//如果index非法,返回-1
if (index < 0 || index >= size) {
return -1;
}
ListNode cur = head;
//第0个节点是虚拟头节点,所以查找第 index+1 个节点
for (int i = 0; i <= index; i++) {
cur = cur.next;
}
return cur.val;
}

public void addAtHead(int val) {
ListNode newNode = new ListNode(val);
newNode.next = head.next;
head.next = newNode;
size++;

// 在链表最前面插入一个节点,等价于在第0个元素前添加
// addAtIndex(0, val);
}


public void addAtTail(int val) {
ListNode newNode = new ListNode(val);
ListNode cur = head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = newNode;
size++;

// 在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加
// addAtIndex(size, val);
}

// 在第 index 个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
// 如果 index 等于链表的长度,则说明是新插入的节点为链表的尾结点
// 如果 index 大于链表的长度,则返回空
public void addAtIndex(int index, int val) {
if (index < 0 || index > size) {
return;
}

//找到要插入节点的前驱
ListNode pre = head;
for (int i = 0; i < index; i++) {
pre = pre.next;
}
ListNode newNode = new ListNode(val);
newNode.next = pre.next;
pre.next = newNode;
size++;
}

public void deleteAtIndex(int index) {
if (index < 0 || index >= size) {
return;
}

//因为有虚拟头节点,所以不用对index=0的情况进行特殊处理
ListNode pre = head;
for (int i = 0; i < index ; i++) {
pre = pre.next;
}
pre.next = pre.next.next;
size--;
}
}