- 自我尝试
- 代码
/** * 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,head); ListNode pre = dummyHead; ListNode cur = dummyHead.next; while(cur!=null){ if(cur.val==val){ // 移除的元素pre不变,cur变 pre.next = cur.next; }else{ // 未移除的话都变 pre = pre.next; } cur = cur.next; } return dummyHead.next; } }
- 收获
- 方法:添加虚拟头节点。俩指针来操控当前node与之前node。要移除的就pre指向cur.next;这时只移动cur,pre不变
- 代码
- 题解心得
- 使用方法一样,皆为头节点。但其添加了一个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) { if(head==null){ return head; } ListNode dummyHead = new ListNode(0,head); ListNode pre = dummyHead; ListNode cur = dummyHead.next; while(cur!=null){ if(cur.val==val){ // 移除的元素pre不变,cur变 pre.next = cur.next; }else{ // 未移除的话都变 pre = pre.next; } cur = cur.next; } return dummyHead.next; } }
- 使用方法一样,皆为头节点。但其添加了一个head不为空的判断
- 自我尝试
- 代码
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 { ListNode head; int size; public MyLinkedList() { this.head = new ListNode(-1); this.size = 0; } public int get(int index) { if(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) { ListNode cur = head; while(cur.next!=null){ cur = cur.next; } cur.next = new ListNode(val); this.size++; } public void addAtIndex(int index, int val) { if(index==this.size){ addAtTail(val); }else if(index>this.size){ //Invalid operation return; }else{ //插入 ListNode pre=this.head; ListNode cur=this.head.next; for(int i=0;i<index;i++){ pre=cur; cur=cur.next; } pre.next = new ListNode(val); pre.next.next = cur; this.size++; } } public void deleteAtIndex(int index) { if(index>=this.size){ // Invalid operation return; } ListNode pre=this.head; for(int i=0;i<index;i++){ pre=pre.next; } pre.next=pre.next.next; this.size--; } } /** * 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); */
- 收获
- 使用dummyhead提供遍历的便捷性
- 引入size属性,在方法的实现中提供便利
- 代码
- 题解心得
- 代码
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 { ListNode head; int size; public MyLinkedList() { this.head = new ListNode(-1); this.size = 0; } public int get(int index) { // 小于0的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) { //直接也使用addAtIndedx addAtIndex(this.size,val); } public void addAtIndex(int index, int val) { if(index>this.size){ //Invalid operation return; }else{ //可以只使用单个指针 ListNode pre=this.head; for(int i=0;i<index;i++){ pre=pre.next; } pre.next = new ListNode(val,pre.next); this.size++; } } public void deleteAtIndex(int index) { if(index>=this.size){ // Invalid operation return; } ListNode pre=this.head; for(int i=0;i<index;i++){ pre=pre.next; } pre.next=pre.next.next; this.size--; } } /** * 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); */
- 收获
-
addAtIndex方法里可以也通过一个指针直接进行操作。没必要两个。
-
- 代码
- 自我尝试
- 代码使用迭代法,通过三个pointer操作
/** * 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 tmp; ListNode cur=head; ListNode pre=null; while(cur!=null){ tmp = cur.next; cur.next = pre; pre=cur; cur = tmp; } return pre; } }
- 代码使用迭代法,通过三个pointer操作
- 题解心得
- 递归法比较难。递进到最底层,确定了新head后,层层传出来!同时在这个过程中进行链接的reverse
/** * 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) { // 边缘条件,传入为null if(head==null){ return null; } // 边缘条件,确定最后一个node,并且返回 if (head.next==null) return head; // 将head层层传递出来通过last ListNode last = reverseList(head.next); //反转 head.next.next = head; //head指为空 head.next = null; System.out.println(last.val); return last; } }
- 递归法比较难。递进到最底层,确定了新head后,层层传出来!同时在这个过程中进行链接的reverse