算法day03
203.移除链表元素
题目链接:https://leetcode.cn/problems/remove-linked-list-elements/ 思路:先判断头是否空;再判断头是否要删;再删其他需要删的非头元素 没有虚拟头节点的代码:
class Solution {
public ListNode removeElements ( ListNode head, int val) {
if ( head== null ) return null ;
while ( head!= null && head. val== val) head = head. next;
ListNode node = head;
while ( node!= null && node. next!= null ) {
if ( node. next. val == val) {
node. next = node. next. next;
} else {
node = node. next;
}
}
return head;
}
}
有虚拟头节点的代码
class Solution {
public ListNode removeElements ( ListNode head, int val) {
ListNode stumpNode = new ListNode ( ) ;
stumpNode. next = head;
ListNode cur = stumpNode;
while ( cur!= null && cur. next!= null ) {
if ( cur. next. val== val) {
cur. next = cur. next. next;
} else {
cur = cur. next;
}
}
return stumpNode. next;
}
}
总结:得多练
707.设计链表
题目链接:https://leetcode.cn/problems/design-linked-list/description/ 还没写出来,只是看过视频跟了下思路 代码实现:
class LinkedNode {
public int val;
public LinkedNode next;
public LinkedNode ( ) {
}
public LinkedNode ( int val) {
this . val = val;
}
}
class MyLinkedList {
private LinkedNode dummyNode;
private int size;
public MyLinkedList ( ) {
size = 0 ;
dummyNode = new LinkedNode ( 0 ) ;
dummyNode. next = null ;
}
public int get ( int index) {
if ( index>= size || index< 0 ) {
return - 1 ;
}
LinkedNode node = dummyNode;
while ( index> 0 ) {
node = node. next;
index-- ;
System . out. println ( node. val) ;
}
return node. next. val;
}
public void addAtHead ( int val) {
LinkedNode node = new LinkedNode ( val) ;
node. next = dummyNode. next;
dummyNode. next = node;
size++ ;
}
public void addAtTail ( int val) {
LinkedNode node = dummyNode;
while ( node. next!= null ) {
node = node. next;
}
LinkedNode newNode = new LinkedNode ( val) ;
node. next = newNode;
newNode. next = null ;
size++ ;
}
public void addAtIndex ( int index, int val) {
if ( index> size || index< 0 ) {
return ;
}
LinkedNode newNode = new LinkedNode ( val) ;
LinkedNode flag = dummyNode;
while ( index> 0 ) {
flag = flag. next;
index-- ;
}
newNode. next = flag. next;
flag. next = newNode;
size++ ;
}
public void deleteAtIndex ( int index) {
if ( index< 0 || index>= size) {
return ;
}
LinkedNode flag = dummyNode;
while ( index> 0 ) {
flag = flag. next;
index-- ;
}
flag. next = flag. next. next;
size-- ;
}
}
206.反转链表
题目链接:https://leetcode.cn/problems/reverse-linked-list/description/ 思路跟上了,代码还没写出来。 自己第一下的思路,头插法,把所有的节点都过一次,然后都头插。 自己写的头插法代码:
class Solution {
public ListNode reverseList ( ListNode head) {
ListNode dummyNode = new ListNode ( ) ;
dummyNode. next = head;
ListNode temp = dummyNode. next;
if ( temp== null ) {
return null ;
}
else if ( temp== head) {
temp = temp. next;
head. next = null ;
}
while ( temp!= null ) {
ListNode node = temp. next;
temp. next = dummyNode. next;
dummyNode. next = temp;
temp = node;
}
return dummyNode. next;
}
}
三个指针,直接原地修改指向的代码:
class Solution {
public ListNode reverseList ( ListNode head) {
if ( head == null ) return null ;
ListNode pre = null ;
ListNode cur = head;
ListNode last = cur. next;
while ( cur!= null ) {
cur. next = pre;
pre = cur;
cur = last;
if ( last!= null ) {
last = last. next;
}
}
return pre;
}
}