赞
踩
链表就是通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域,另一个为指针域,其指向下一个节点,最后一个节点指向null。
public class ListNode{ //结点的值 int val; //下一个结点 ListNode next; //无参构造 public ListNode(){ } //一个参数 public ListNode(int val){ this.val = val; } //两个参数 public ListNode(int val, ListNode next){ this.val = val; this.next = next; } }
给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
class Solution { public ListNode removeElements(ListNode head, int val) { while(head != null && head.val == val){ head = head.next; } if(head == null){ return head; } ListNode preNode = head; ListNode curNode = head.next; while(curNode != null){ if(curNode.val == val){ preNode.next = curNode.next; }else{ preNode = curNode; } curNode = curNode.next; } return head; } }
class ListNode { int val; ListNode next; ListNode(){} ListNode(int val) { this.val=val; } } class MyLinkedList { //size存储链表元素的个数 int size; //虚拟头结点 ListNode head; //初始化链表 public MyLinkedList() { size = 0; head = new ListNode(0); } //获取第index个节点的数值 public int get(int index) { //如果index非法,返回-1 if (index < 0 || index >= size) { return -1; } ListNode currentNode = head; //包含一个虚拟头节点,所以查找第 index+1 个节点 for (int i = 0; i <= index; i++) { currentNode = currentNode.next; } return currentNode.val; } //在链表最前面插入一个节点 public void addAtHead(int val) { addAtIndex(0, val); } //在链表的最后插入一个节点 public void addAtTail(int val) { addAtIndex(size, val); } // 在第 index 个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。 // 如果 index 等于链表的长度,则说明是新插入的节点为链表的尾结点 // 如果 index 大于链表的长度,则返回空 public void addAtIndex(int index, int val) { if (index > size) { return; } if (index < 0) { index = 0; } size++; //找到要插入节点的前驱 ListNode pred = head; for (int i = 0; i < index; i++) { pred = pred.next; } ListNode toAdd = new ListNode(val); toAdd.next = pred.next; pred.next = toAdd; } //删除第index个节点 public void deleteAtIndex(int index) { if (index < 0 || index >= size) { return; } size--; ListNode pred = head; for (int i = 0; i < index; i++) { pred = pred.next; } pred.next = pred.next.next; } } /** * Your MyLinkedList object will be instantiated and called as such: * MyLinkedList obj = new MyLinkedList(); * int param_1 = obj.get(index); * obj.addAtHead(val); * obj.addAtTail(val); * obj.addAtIndex(index,val); * obj.deleteAtIndex(index); */
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
分析:
/** * 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 reverseList(ListNode head) { ListNode pre = null; ListNode cur = head; ListNode temp = null; while(cur != null){ temp = cur.next; cur.next = pre; pre = cur; cur = temp; } return pre; } } //递归 class Solution { public ListNode reverseList(ListNode head) { return reverseHelper(null, head); } private ListNode reverseHelper(ListNode pre, ListNode cur){ if(cur == null){ return pre; } ListNode temp = null; temp = cur.next; cur.next = pre; return reverseHelper(cur, temp); } }
有点难度
/** * 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 swapPairs(ListNode head) { if(head == null || head.next == null){ return head; } ListNode dummyNode = new ListNode(0); dummyNode.next = head; ListNode pre = dummyNode; while(pre.next != null && pre.next.next != null){ ListNode temp = head.next.next; pre.next = head.next; head.next.next = head; head.next = temp; pre = head; head = head.next; } return dummyNode.next; } }
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
public ListNode removeNthFromEnd(ListNode head, int n) { ListNode fakeNode = new ListNode(0); fakeNode.next = head; ListNode fast = fakeNode; ListNode slow = fakeNode; for(int i = 0; i < n; i++){ fast = fast.next; } while(fast.next != null){ fast = fast.next; slow = slow.next; } slow.next = slow.next.next; return fakeNode.next; } }
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode curA = headA; ListNode curB = headB; int lenA = 0 , lenB = 0; while(curA != null){ curA = curA.next; lenA++; } while(curB != null){ curB = curB.next; lenB++; } curA = headA; curB = headB; //让curA为最长链表的头,lenA为其长度 if (lenB > lenA){ //1.swap(lenA,lenB); int tmpLen = lenA; lenA = lenB; lenB = tmpLen; //2.swap(curA,curB); ListNode tmpNode = curA; curA = curB; curB = tmpNode; } //求长度差 int gap = lenA - lenB; //让curA和curB在同一起点位置上(末尾位置对其) while (gap-- > 0){ curA = curA.next; } //遍历curA和curB,遇到相同的值即返回 while (curA != null){ if (curA == curB){ return curA; } curA = curA.next; curB = curB.next; } return null; } }
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode detectCycle(ListNode head) { ListNode slow = head; ListNode fast = head; while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next; if(slow == fast){ ListNode index1 = fast; ListNode index2 = head; while(index1 != index2){ index1 = index1.next; index2 = index2.next; } return index1; } } return null; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。