数据结构与算法之leetcode数组和链表

203.移除链表元素

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode RemoveElements(ListNode head, int val) {
        //移除链表中值==val   
        //1.移除首部==val的值
        while (head != null && head.val == val)
        {
            head = head.next;
        }
        if (head == null)
        {
            return head;
        }
        //头部所有==val已经去除   头节点一定不等于 val
        ListNode pre = head;
        while (pre.next!= null)
        {
            if (pre.next.val!=val)
            {
                pre = pre.next;
            }
            else
            {
                pre.next = pre.next.next;
            }
        }
        return head;
    }
}

876.链表的中间结点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode MiddleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null)
        {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

83. 删除排序链表中的重复元素

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode DeleteDuplicates(ListNode head) {
        if (head == null || head.next == null)
        {
            return head;
        }
        // 1 - 1 - 2 - 2 - 3 - 4
        ListNode cur = head;
        ListNode next = head.next;
        while (next != null)
        {
            if (cur.val == next.val)
            {
                next = next.next;
                cur.next = next;
            }
            else
            {
                next = next.next;
                cur = cur.next;
            }
        }
        return head;
    }
}

剑指 Offer 25. 合并两个排序的链表

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

2. 两数相加

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
    {
        ListNode newList = new ListNode(0);
        ListNode cur = newList;
        bool CarryBit = false;
        while (l1 != null || l2 != null)
        {
            int n1 = l1 == null ? 0 : l1.val;
            int n2 = l2 == null ? 0 : l2.val;
            int bitNum = 0;
            if (CarryBit)
            {
                bitNum = CarryBit ? 1 : 0;
                CarryBit = false;
            }
            if ((n1 + n2 + bitNum) >= 10)
            {
                CarryBit = true;
            }
            cur.next = new ListNode((n1 + n2 + bitNum) % 10);
            if (l1 != null)
            {
                l1 = l1.next;
            }
            if (l2 != null)
            {
                l2 = l2.next;
            }
            cur = cur.next;
        }
        if (CarryBit)
        {
            cur.next = new ListNode(1);
        }
        return newList.next;
    }
}

206. 反转链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode ReverseList(ListNode head) {
        // 1 - 2 - 3 - 4 - 5
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null)
        {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}

234. 回文链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
public ListNode ReverseList(ListNode head)
    {
        // 1 - 2 - 3 - 4 - 5
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null)
        {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
    private ListNode EndHalfPartListNode(ListNode head)
    {
        // 1 2 3 4 5 6   slow 算在前半部分
        ListNode fast = head, slow = head;
        while (fast.next != null && fast.next.next != null)
        {
            slow = slow.next;
            fast = fast.next.next;
        }
        //后半部分的头节点
        return slow;
    }
    public bool IsPalindrome(ListNode head)
    {
        ListNode halfNode = EndHalfPartListNode(head);
        //翻转后半部分
        ListNode halfReveredHead = ReverseList(halfNode.next);
        //翻转完毕
        ListNode p1 = head;
        ListNode p2 = halfReveredHead;
        while (p2 != null)
        {
            if (p1.val != p2.val)
            {
                return false;
            }
            p1 = p1.next;
            p2 = p2.next;
        }
        halfNode.next = ReverseList(halfReveredHead);
        return true;
    }
}

328. 奇偶链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode OddEvenList(ListNode head) {
        if (head == null || head.next == null)
        {
            return head;
        }
        // 1 2 3 4 5
        // 1 3 5 2 4
        ListNode odd = head;
        ListNode evenHead = head.next;
        ListNode even = evenHead;
        while (even != null && even.next != null)
        {
            odd.next = even.next;
            odd = odd.next;
            even.next = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }
}

25. K 个一组翻转链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode ReverseKGroup(ListNode head, int k)
    {
        //分组翻转
        //判断边界条件 head.length < k  不做翻转
        ListNode hair = new ListNode(0);
        hair.next = head;
        ListNode pre = hair;
        while (head != null)
        {
            ListNode tail = pre;
            for (int i = 0; i < k; i++)
            {
                tail = tail.next;
                if (tail == null)
                {
                    return hair.next;
                }
            }
            //记录下一组的节点
            ListNode next = tail.next;
            ListNode[] reverse = MyReverseList(head, tail);
            //接入
            head = reverse[0];
            tail = reverse[1];
            //把子链表重新装回原链表
            pre.next = head;
            tail.next = next;
            pre = tail;
            head = tail.next;
        }
        return hair.next;
    }
    public ListNode[] MyReverseList(ListNode head, ListNode tail)
    {
        // 1 2 3 4
        ListNode pre = tail.next;
        ListNode cur = head;
        while (pre != tail)
        {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return new ListNode[] { tail, head };
    }
}

剑指 Offer 22. 链表中倒数第k个节点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode GetKthFromEnd(ListNode head, int k) {
        ListNode fast = head;
        ListNode slow = head;
        while (k > 0)
        {
            k--;
            fast = fast.next;
        }
        while (fast != null)
        {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}

19. 删除链表的倒数第 N 个结点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode RemoveNthFromEnd(ListNode head, int n) {
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        ListNode fast = head;
        ListNode slow = dummyNode;
        for (int i = 0; i < n; i++)
        {
            fast = fast.next;
        }
        while (fast!=null)
        {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummyNode.next;
    }
}

160. 相交链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null)
        {
            return null;
        }

        ListNode lista = headA;
        ListNode listb = headB;
        while (lista != listb)
        {
            if (lista != null)
            {
                lista = lista.next;
            }
            else
            {
                lista = headB;
            }
            if (listb != null)
            {
                listb = listb.next;
            }
            else
            {
                listb = headA;
            }
        }
        return lista;
    }
}

141. 环形链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public bool HasCycle(ListNode head) {
        if (head == null || head.next == null)
        {
            return false;
        }
        ListNode fast = head.next;
        ListNode slow = head;
        while (fast != slow)
        {
            if (fast == null || fast.next == null)
            {
                return false;
            }
            fast = fast.next.next;
            slow = slow.next;
        }
        返回 真;} } ```
    



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值