1.删除指定元素
/**
* 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 dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode p = dummyHead;
while(p.next!=null){
if(p.next.val==val) p.next=p.next.next;
else p = p.next;
}
return dummyHead.next;
}
}
2.设计链表
class ListNode{
int val;
ListNode next;
public ListNode(int val){
this.val = val;
}
}
class MyLinkedList {
int size;
ListNode head;
public MyLinkedList() {
size = 0;
head = new ListNode(0);
}
public int get(int index) {
if(index<0||index>=size){
return -1;
}
ListNode cur = head;
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) return;
index = Math.max(0,index);
size++;
ListNode pred = head;
for(int i=0;i<index;i++) pred = pred.next;
ListNode toAdd = new ListNode(val);
toAdd.next = pred.next;
pred.next = toAdd;
}
public void deleteAtIndex(int index) {
if(index<0 || index>=size) return;
size--;
ListNode pred = head;
for(int i=0;i<index;i++) pred=pred.next;
pred.next = pred.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);
*/
3.反转链表
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pred = null,cur = head;
while(cur!=null){
ListNode next = cur.next;
cur.next = pred;
pred = cur;
cur = next;
}
return pred;
}
}