本文主要是介绍leetcode算法基础 第三条 双指针,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
双指针
- 82. 删除排序链表中的重复元素 II
- [15. 三数之和](https://blog.csdn.net/qq_43788026/article/details/119736076)
82. 删除排序链表中的重复元素 II
//双指针
public ListNode deleteDuplicates(ListNode head) {
if (head ==null || head.next == null){
return head;
}
ListNode re = new ListNode();
re.next = head;
ListNode pre = re;
ListNode cur = head;
while (cur != null && cur.next !=null) {
if (cur.val == cur.next.val) {
while (cur.next != null && cur.val == cur.next.val) {
cur = cur.next;
}
pre.next = cur.next;
cur = cur.next;
} else {
pre = cur;
cur = cur.next;
}
}
return re.next;
}
//递归
public ListNode deleteDuplicates1(ListNode head) {
if (head == null) return head;
if (head.next != null && head.val == head.next.val) {
while (head.next != null && head.val == head.next.val) {
head = head.next;
}
return deleteDuplicates1(head.next);
}else {
head.next = deleteDuplicates1(head.next);
}
return head;
}
15. 三数之和
这篇关于leetcode算法基础 第三条 双指针的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!