82. 删除排序链表中的重复元素 II
/**
* 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 deleteDuplicates(ListNode head) {
//自己写得方法不会处理头结点
//找到一个重复时利用此值遍历,完成删除,
ListNode cur=new ListNode(0,head);
ListNode res=cur;
while(cur.next!=null&&cur.next.next!=null){
if(cur.next.val==cur.next.next.val){
int x=cur.next.val;
while(cur.next!=null&&cur.next.val==x){
cur.next=cur.next.next;
}
//[1,1,2,2]
}else{
cur=cur.next;
}
}
return res.next;
}
}
86. 分隔链表
/**
* 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 partition(ListNode head, int x) {
//思路正确,就是用两个链表进行拼接
//但创建链表时没有想到直接用原链表节点就可以完成拼接
ListNode small =new ListNode(0);
ListNode big=new ListNode(0);
ListNode dummysmall=small;
ListNode dummybig=big;
while(head!=null){
if(head.val<x){
small.next=head;
small=small.next;
}else{
big.next=head;
big=big.next;
}
head=head.next;
}
big.next=null;
small.next=dummybig.next;
return dummysmall.next;
}
}
92. 反转链表 II
/**
* 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 reverseBetween(ListNode head, int left, int right) {
//1.没有思路,没有想到头插法
ListNode p1=new ListNode(0,head);
ListNode p2=head;
ListNode p3=p1;
for(int i=0;i<left-1;i++){
p1=p1.next;
p2=p2.next;
}
for(int i=0;i<right-left;i++){
ListNode temp=p2.next;
p2.next=p2.next.next;
//头插法总结:
// 每一个新节点的next始终指向头结点的next;
//头结点的next始终指向新结点!!!!
temp.next=p1.next;
p1.next=temp;
}
//注意如果头结点也发生变化,则返回头节点就不对了,所以利用一个虚拟节点返回
//return head;
return p3.next;
}
}
876. 链表的中间结点
/**
* 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 middleNode(ListNode head) {
ListNode slow=new ListNode(0,head);
ListNode fast=new ListNode(0,head);
while(fast!=null){
slow=slow.next;
if(fast.next==null||fast.next.next==null){
break;
}
fast=fast.next.next;
}
return slow;
}
}
217. 存在重复元素
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set=new HashSet<>();
for(int i=0;i<nums.length;i++){
if(set.contains(nums[i])){
return true;
}else{
set.add(nums[i]);
}
}
return false;
}
}
剑指 Offer 03. 数组中重复的数字
class Solution {
public int findRepeatNumber(int[] nums) {
HashSet<Integer> set=new HashSet<>();
int res=0;
for(int i=0;i<nums.length;i++){
if(set.contains(nums[i])){
res=nums[i];
break;
}else{
set.add(nums[i]);
}
}
return res;
}
}
137. 只出现一次的数字 II
class Solution {
public int singleNumber(int[] nums) {
Arrays.sort(nums);
int res=0;
int flag=0;
for(int i=0;i<nums.length-3;i=i+3){
if(nums[i]!=nums[i+1]){
res=nums[i];
flag=1;
break;
}
}
if(flag==0) return nums[nums.length-1];
return res;
}
}