实现单链表的基本操作(1)

1.删除链表中指定的所有元素
(https://leetcode-cn.com/problems/removelinked-list-elements/description/)

   public ListNode reverseList(ListNode head) {
        if(head == null) {
                return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode newhead = null;
        ListNode cur = head;
        ListNode prev = null;
        while(cur != null) {
            ListNode next = cur.next;
            if(next == null) {
                newhead = cur;
            }
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return newhead;
    }

2.反转一个单链表
(https://leetcode-cn.com/problems/reverse-linkedlist/description/)

public ListNode removeElements(ListNode head, int val) {
    if(head == null) {
        return null;
    }
    ListNode prev = head;
    ListNode node = head.next;
    while(node != null) {
        if(node.val == val) {
            prev.next = node.next;
            node = prev.next;
        }else {
            prev = node;
            node = node.next;
        }
    }
    if(head.val == val) {
        head = head.next;
    }
    return head;
}

3.给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
(https://leetcode-cn.com/problems/middle-of-the-linked-list/description/)

public ListNode middleNode(ListNode head) {
    int steps = size(head)/2;
    ListNode cur = head;
    for(int i = 0;i < steps;i++) {
        cur = cur.next;
    }
    return cur;
}
public int size(ListNode head) {
    int size = 0;
    for(ListNode cur = head;cur != null;cur = cur.next) {
        size++;
    }
    return size;
}

4.输入一个链表,输出该链表中倒数第k个结点
(https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a? tpId=13&&tqId=11167&rp=2&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking)

public ListNode FindKthToTail(ListNode head,int k) {
    int length = size(head);
    if(head == null || k <= 0 || k > length) {
        return null;
    }
    int offset = length - k;
    ListNode cur = head;
    for(int i = 0;i < offset;i++) {
        cur = cur.next;
    }
    return cur;
}
public int size(ListNode head) {
    int size = 0;
    for(ListNode cur = head;cur != null;cur = cur.next) {
        size++;
    }
    return size;
}

5.将两个有序链表合并为一个新的有序链表并返回
(https://leetcode-cn.com/problems/merge-two-sorted-lists/description/)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null ) {
            return l2;
        }
        if(l2 == null) {
            return l1;
        }
        ListNode newlist = new ListNode(0);
        ListNode newtail = newlist;
        while(l1 != null && l2 != null) {
            if(l1.val < l2.val) {
                newtail.next = new ListNode(l1.val);
                l1 = l1.next;
                newtail = newtail.next;
            }else{
                newtail.next = new ListNode(l2.val);
                l2 = l2.next;
                newtail = newtail.next;
             }
        }
        if(l1 != null) {
            newtail.next = l1;
        }else{
            newtail.next = l2;
        }
         return newlist.next;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值