Day3
LeetCode203 移除链表元素
题目链接:移除链表元素
/**
* 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) {
while(head != null && head.val == val) head = head.next;
if(head == null) return null;
ListNode curr = head;
while(curr.next != null){
if(curr.next.val == val) curr.next = curr.next.next;
else curr = curr.next;
}
return head;
}
}
/**
* 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) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode curr = dummy;
while(curr.next != null){
if(curr.next.val == val) curr.next = curr.next.next;
else curr = curr.next;
}
return dummy.next;
}
}
LeetCode707 设计链表
题目链接:设计链表
/*
单链表
*/
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 MyLinkedList {
int size;
ListNode head;
public MyLinkedList() {
size = 0;
head = new ListNode();
}
public int get(int index) {
if (index < 0 || index >= size) {
return -1;
}
ListNode curr = head;
while(index-- >= 0) curr= curr.next;
return curr.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 || index < 0) {
return;
}
size++;
ListNode prev = head;
while(index-- > 0) prev = prev.next;
prev.next = new ListNode(val, prev.next);
}
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) {
return;
}
size--;
ListNode prev = head;
while(index-- > 0) prev = prev.next;
prev.next = prev.next.next;
}
}
/*
双链表
*/
class ListNode{
int val;
ListNode next,prev;
ListNode() {};
ListNode(int val){
this.val = val;
}
}
class MyLinkedList {
int size;
ListNode head,tail;
public MyLinkedList() {
this.size = 0;
this.head = new ListNode(0);
this.tail = new ListNode(0);
head.next=tail;
tail.prev=head;
}
public int get(int index) {
if(index>=size || index < 0) return -1;
ListNode cur = this.head;
if(index >= size / 2){
cur = tail;
for(int i=0; i< size-index; i++){
cur = cur.prev;
}
}else{
for(int i=0; i<= index; i++){
cur = cur.next;
}
}
return cur.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 || index < 0) return;
size++;
ListNode prev = this.head;
for(int i=0; i<index; i++){
prev = prev.next;
}
ListNode newNode = new ListNode(val);
newNode.next = prev.next;
prev.next.prev = newNode;
newNode.prev = prev;
prev.next = newNode;
}
public void deleteAtIndex(int index) {
if(index>=size || index < 0) return;
size--;
ListNode prev = this.head;
for(int i=0; i<index; i++){
prev = prev.next;
}
prev.next.next.prev = prev;
prev.next = prev.next.next;
}
}
LeetCode206 反转链表
题目链接:反转链表
/**
* 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) {
if(head == null) return null;
ListNode prev = null;
ListNode curr = head;
while(curr != null){
ListNode nextNode = curr.next;
curr.next = prev;
prev = curr;
curr = nextNode;
}
return prev;
}
}

1269

被折叠的 条评论
为什么被折叠?



