实现链表以下操作
目录
1.给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次
4. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
1.给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次
示例 1:
输入: 1->1->2
输出: 1->2
示例 2:
输入: 1->1->2->3->3
输出: 1->2->3
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
public ListNode deleteDuplicates(ListNode head) {
ListNode cur = head;
while(cur != null && cur.next != null){
if(cur.val == cur.next.val){
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return head;
}
2. 删除链表中指定的所有元素
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5
示例 2:
输入: 1->1->1->2->3
输出: 2->3
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode nextNode = head.next;
if( head.val == head.next.val){
while(nextNode != null && head.val == nextNode.val){
nextNode = nextNode.next;
}
head = deleteDuplicates(nextNode);
} else{
head.next = deleteDuplicates(nextNode);
}
return head;
}
3. 反转一个单链表
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
public ListNode reverseList(ListNode head) {
if(head == null){
return null;
}
if(head.next == null){
return head;
}
ListNode curNode = head;
ListNode prevNode = null;//记录新链表的头节点
while(curNode != null){
ListNode nextNode = curNode.next;//记录当前节点的后继节点
curNode.next = prevNode;
prevNode = curNode;
curNode = nextNode;
}
return prevNode;
}
4. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public ListNode middleNode(ListNode head) {
if(head == null){
return null;
}
int length = size(head);//链表的长度
int steps = length/2;//找到中间节点时,需要走过的步长
ListNode curNode = head;
for(int i = 0; i < steps;i++){
curNode = curNode.next;
}
return curNode;
}
public int size(ListNode head){//求链表长度的方法
if(head == null){
return 0;
}
int size = 0;
for(ListNode curNode = head;curNode != null;curNode = curNode.next){
size++;
}
return size;
}
}
5. 输入一个链表,输出该链表中倒数第k个结点
https://www.nowcoder.com/questionTerminal/529d3ae5a407492994ad2a246518148a
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null){//链表为空格
return null;
}
int len = size(head);//链表的元素个数
if(k < 0 || k > len){
return null;
} //检验参数k的合法性(容易被遗忘的一个步骤)
int steps = len - k;//链表遍历至倒数第K个结点需要走过的步长
ListNode curNode = head;
for (int i = 0;i < steps;i++){
curNode = curNode.next;
}
return curNode;
}
public int size(ListNode head){
if(head == null){
return 0;
}
int size = 0;
for (ListNode curNode = head;curNode != null; curNode = curNode.next){
size ++;
}
return size;
}
}
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null){
return l2;
}
if(l2 == null){
return l1;
}
ListNode cur1 = l1;
ListNode cur2 = l2;
ListNode newHead = new ListNode(-1);//合并之后的新链表的头结点
ListNode tail = newHead;
while(cur1 != null && cur2 != null){//循环条件要仔细判断
if(cur1.val < cur2.val){//
tail.next = cur1;//在新链表的尾部插入元素
cur1 = cur1.next;//更新原来链表,让cur1指向被插入新链表元素的下一个
tail = tail.next;//更新新链表的尾部结点,以便插入新的元素
} else {
tail.next = cur2;//在新链表的尾部插入元素
cur2 = cur2.next;
tail = tail.next;
}
}
if(cur1 == null && cur2 != null){
tail.next = cur2;
}
if(cur2 == null && cur1 != null){
tail.next = cur1;
}
return newHead.next; //返回newHead.next newHead指向的是一个头结点
}
}