leetcode203 移除链表元素
注意虚拟头节点的应用,创建一个虚拟头节点并指向原链表本身的head节点。利用pre和cur指针,遍历链表并进行链表节点的删除操作。当符合删除条件时,只移动cur指针;当不满足条件时,pre和cur指针同时前移一个节点。函数最后返回虚拟头节点的next节点。
/**
* 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(-1,head);
ListNode pre = dummy;
ListNode cur = head;
while(cur != null){
if(cur.val == val){
pre.next = cur.next;
}else{
pre = cur;
}
cur = cur.next;
}
return dummy.next;
}
}
设计链表
注意循环变量要在循环体内改变,不然会形成死循环。注意在前一个循环体中修改的指针的位置,其是否还适合于下一次循环的起始位置。注意addAtTail()方法在链表为空时的特例的处理方式。
class Node{
public int val;
public Node next;
public Node(){};
public Node(int val){
this.val = val;
}
public Node(int val, Node next){
this.val = val;
this.next = next;
}
}
class MyLinkedList {
private Node head;
public MyLinkedList() {
Node dummy = new Node(-1);
}
public int get(int index) {
Node cur = head;
int length = 0;
while(cur != null){
cur = cur.next;
length++;
}
cur = head;
if(index >= length || index < 0){
return -1;
}else{
if(index == 0){
return head.val;
}
while(index > 0){
cur = cur.next;
index--;
}
return cur.val;
}
}
public void addAtHead(int val) {
Node headNew = new Node(val,head);
head = headNew;
}
public void addAtTail(int val) {
if(head == null){
head = new Node(val);
}
else{
Node cur = head;
while(cur.next != null){
cur = cur.next;
}
cur.next = new Node(val,null);
}
}
public void addAtIndex(int index, int val) {
int length = 0;
Node cur = head;
while(cur != null){
cur = cur.next;
length++;
}
cur = head;
if(index == 0){
addAtHead(val);
}
if(index == length){
addAtTail(val);
}
if(index > 0 && index <length){
while((index-1) > 0){
cur = cur.next;
index--;
}
Node reserve = cur.next;
cur.next = new Node(val,reserve);
}
}
public void deleteAtIndex(int index) {
int length = 0;
Node cur = head;
while(cur != null){
cur = cur.next;
length++;
}
cur = head;
if(index == 0){
head = head.next;
}
if(index > 0 && index < length){
while((index-1) > 0){
cur = cur.next;
index--;
}
cur.next = cur.next.next;
}
}
}
/**
* 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);
*/
leetcode 206 反转链表
注意虚拟头节点的应用。定义三个指针:pre,cur,next。
/**
* 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 dummy = new ListNode(-1,null);
ListNode pre = dummy;
ListNode cur = head;
ListNode next = cur.next;
while(next != null){
cur.next = pre;
pre = cur;
cur = next;
next = next.next;
}
cur.next = pre;
head.next = null;
return cur;
}
}