我新建的个人博客,欢迎访问:hmilzy.github.io


203.移除链表元素

题目链接: 移除链表元素

若要移除链表中某一个结点,需要分为两种情况:
1.移除中间结点:让上一个结点next指向下一个结点
2.移除头结点,则直接让头结点后移即可
3.因此,为了想让链表移除结点操作逻辑统一,则可以加上一个虚拟头结点,也就是让虚拟头结点dummyhead的 next 指向头结点head。那么这样的话,移除头结点也只需要将虚拟头结点指向头结点的下一个结点就行了,逻辑得到统一。
4.而且,一般链表类题目的返回值是链表的头结点,题目中难以去额外保存一个头结点。如果设置了虚拟头结点的话,那么就可以直接返回dummyhead.next

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
/**
* 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 dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode pre = dummyNode;
ListNode cur = head;

while(cur != null){

if(cur.val == val){
pre.next = cur.next;
}else{
pre = cur;
}
cur = cur.next;
}
return dummyNode.next;
}
}

707.设计链表

题目链接: 设计链表

设计链表逻辑比较多,主要是一些index边界条件需要额外判判断。此处我设计的是单链表

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
class ListNode{
int val;
ListNode next;
public ListNode(){}
public ListNode(int val){
this.val = val;
}
public ListNode(int val,ListNode next){
this.val = val;
this.next = next;
}
}

class MyLinkedList {

int size;
ListNode dummyhead;

public MyLinkedList() {
size = 0;
dummyhead = new ListNode(0);
}

public int get(int index) {
if(index < 0 || index >= size){
return -1;
}
ListNode currentNode = dummyhead;
for(int i = 0; i <= index; i++){
currentNode = currentNode.next;
}
return currentNode.val;
}

public void addAtHead(int val) {
addAtIndex(0, val);
}

public void addAtTail(int val) {
addAtIndex(size,val);
}

public void addAtIndex(int index, int val) {
if(index > size){
return;
}
if(index < 0){
index = 0;
}
ListNode preNode = dummyhead;
for(int i = 0; i < index; i++){
preNode = preNode.next;
}
ListNode addNode = new ListNode(val);
addNode.next = preNode.next;
preNode.next = addNode;
size++;
}

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

ListNode preNode = dummyhead;
for(int i = 0; i < index; i++){
preNode = preNode.next;
}
preNode.next = preNode.next.next;
size--;
}
}

/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/

206. 反转链表

题目链接: 反转链表

不要去新建一个链表,而只需要在原来的链表上操作
定义pre cur temp 三个ListNode,实现链表反转

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
/**
* 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 reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
ListNode temp = null;

while(cur != null){
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}