203.移除链表元素
题目链接:移除链表元素
题目内容:
删除链表中等于给定值 val 的所有节点。
示例 1: 输入:head = [1,2,6,3,4,5,6], val = 6 输出:[1,2,3,4,5]
示例 2: 输入:head = [], val = 1 输出:[]
示例 3: 输入:head = [7,7,7,7], val = 7 输出:[]
这是链表的初级题目了,还算简单,用了我理解的虚拟头链表写的
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode newhead = new ListNode(1);
newhead.next = head;
ListNode current = newhead;
while(current != null){
if(current.next != null && current.next.val == val){
current.next = current.next.next;
}
else current = current.next;
}
ListNode headnow = newhead.next;
newhead.next = null;
return headnow;
}
}
707.设计链表
题目链接:设计链表
题目内容:
在链表类中实现这些功能:
- get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
- addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
- addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
- addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
- deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
这是我的代码,我的代码特点就是一个混乱,其实还是答案里的采用虚拟头结的方法更可靠。总之我这个代码修修改改也能用。
class ListNode {
int val;
ListNode next;
ListNode(){}
ListNode(int val) {
this.val=val;
}
}
class MyLinkedList {
ListNode mylist;
int length = 0;
ListNode head = mylist;
public MyLinkedList() {
mylist = null;
}
public int get(int index) {
ListNode find = head;
if(find == null)return -1;
if(index < 0 || index > length)return -1;
for(int i = 0; i < index; i++){
find = find.next;
if(find == null){
return -1;
}
}
return find.val;
}
public void addAtHead(int val) {
ListNode newhead = new ListNode(1);
newhead.next = head;
newhead.val = val;
head = newhead;
length++;
}
public void addAtTail(int val) {
ListNode newnode = new ListNode(1);
newnode.val = val;
ListNode find = head;
if(length == 0){
head = newnode;
length++;
return;
}
for(int i = 0;i < length-1; i++){
find = find.next;
}
find.next = newnode;
length++;
}
public void addAtIndex(int index, int val) {
if(index < 0 || index > length)return ;
ListNode newnode = new ListNode(1);
newnode.val = val;
ListNode find = head;
if(index == 0){
newnode.next = head;
head = newnode;
length++;
return;
}
if(index == length){
for(int i = 0; i < index - 1; i++){
find = find.next;
}
find.next = newnode;
length++;
return;
}
for(int i = 0; i < index - 1; i++){
find = find.next;
if(find.next== null)return;
}
newnode.next = find.next;
find.next = newnode;
length++;
}
public void deleteAtIndex(int index) {
ListNode find = head;
if(index < 0 || index > length)return ;
if(index == 0){
head = head.next;
length--;
}
for(int i = 0; i < index - 1; i++){
if(find != null && find.next != null){
find = find.next;
}
else return;
}
if(find.next != null){
find.next = find.next.next;
length--;
}
else return;
}
}
206.反转链表
题目链接:反转链表
题意:反转一个单链表。
示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
自己想的办法由于会构成环形链表,被报错不能运行了,就看了一下提供的办法,还挺简单容易写的
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = head;
ListNode cur = null;
ListNode temp = null;
if(head != null && head.next != null) cur = head.next;
else return pre;
pre.next = null;
while(cur != null){
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
思想就是双指针不断后移就好了,然后不断一个个改变链表方向。