链表及其应用(2)

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;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值