leetcode官网中的一些关于链表题目的解答:
结点类:
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
反转链表
public ListNode reverseList(ListNode head) {
// head->5->4->3->2->1->NULL
// newHead->null
ListNode newHead=null;
while (head!=null){
ListNode temp=head.next;
head.next=newHead;
newHead=head;
head=temp;
}
return newHead;
}
判断相交链表
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA == null || headB == null) return null;
ListNode listNodeA=headA;
ListNode listNodeB=headB;
while (listNodeA!=listNodeB){
listNodeA= (listNodeA==null ? headB : listNodeA.next);
listNodeB= (listNodeB==null ? headA : listNodeB.next);
}
return listNodeA;
}
判断链表是否有环并返回第一次相交的节点
public ListNode detectCycle(ListNode head) {
//1.判断链表是否有环
ListNode fast=head;
ListNode slow=head;
boolean flag=false;
while (fast!=null && fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if (slow==fast){
flag=true;
break;
}
}
//2.如果有环,返回第一个节点,没有环返回为空
if (flag){
ListNode temp=head;
while (temp!=slow){
temp=temp.next;
slow=slow.next;
}
return temp;
}else {
return null;
}
}
删除链表的倒数第 N 个结点
public ListNode removeNthFromEnd(ListNode head, int n) {
//head->1->2->3->4->5->null
//head->1->null
//head->1->2->null
if(head==null || head.next==null) return null;
ListNode fast = head, slow = head;
for(int i = 0; i < n; i++){
fast = fast.next;
}
//fast为前一个节点
if(fast==null){
return head.next;
}
while(fast.next!=null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
两个链表相加
//两个链表相加
//l1->1->4->3->null
//l2->5->6->4->null
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//root->null
ListNode root = new ListNode(0);
ListNode cursor=root;
int carry=0;//用于存储进位
while (l1!=null || l2!=null ||carry!=0){
int l1val= (l1!= null ? l1.val:0);
int l2val= (l2!= null ? l2.val:0);
int sumVal=l1val+l2val+carry;
carry=sumVal/10;
ListNode newNode= new ListNode(sumVal%10);
cursor.next=newNode;
cursor=newNode;
if (l1!=null){
l1=l1.next;
}
if (l2!=null){
l2=l2.next;
}
}
return root.next;
}
合并两个有序的链表
//1->2->4->null
//1->3->4->null
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode newNode = new ListNode(0);
ListNode temp=newNode;
//newNode->null
int newVal=0;
while (l1!=null&&l2!=null){
if (l1.val>=l2.val){
ListNode addNode = new ListNode(l2.val);
temp.next=addNode;
temp=addNode;
l2=l2.next;
}else {
ListNode addNode = new ListNode(l1.val);
temp.next=addNode;
temp=addNode;
l1=l1.next;
}
}
while (l1==null&&l2!=null){
//l2有剩余
ListNode addL2Node = new ListNode(l2.val);
temp.next=addL2Node;
temp=addL2Node;
l2=l2.next;
}
while (l2==null&&l1!=null){
//l2有剩余
ListNode addL1Node = new ListNode(l1.val);
temp.next=addL1Node;
temp=addL1Node;
l1=l1.next;
}
return newNode.next;
}
删除有序链表中重复的值
public ListNode deleteDuplicates(ListNode head) {
//head->1->1->2->null
if (head==null){
return null;
}
ListNode temp=head;
while (temp.next!=null){
if(temp.val==temp.next.val){
temp.next=temp.next.next;
}else {
temp=temp.next;
}
}
return head;
}
删除链表中的一个节点
public ListNode deleteNode(ListNode head, int val) {
//head->4->5->1->9->null
//双指针
ListNode slow=head;
ListNode fast=head.next;
if (head.val==val){
head=head.next;
}
while (fast!=null){
if (fast.val==val){
slow.next=fast.next;
break;
}else {
slow=slow.next;
fast=fast.next;
}
}
return head;
}
删除链表中的一个结点二
public void deleteNode(ListNode node) {
if (node!=null){
node.val=node.next.val;
node.next=node.next.next;
}
}
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)
public static int[] reversePrint(ListNode head) {
//反向数组
int count=0;
ListNode temp=head;
while (temp!=null){
count++;
temp=temp.next;
}
temp=head;
int arr[]=new int[count];
for (int i=count-1;i>=0;i--){
arr[i]=temp.val;
temp=temp.next;
}
return arr;
}
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode slow=head;
ListNode fast=head;
for (int i=0;i<k;i++){
fast=fast.next;
}
while (fast!=null){
slow=slow.next;
fast=fast.next;
}
return slow;
}
总结
理解好链表的数据结构,灵活运用技巧
1.技巧一:如何使用快慢指针解决问题
2.技巧二:如何使用双指针解决问题
3.技巧三:用递归解决问题,递归时先想结果,在判断结束条件
4.技巧四:多敲多练
中国成百上千万码友都在努力,你努力了,你也很强。。。。待更新