LeetCode--链表

19. Remove Nth Node From End of List

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode removeNthFromEnd(ListNode head, int n) {
11         if (head == null || head.next == null) {
12             return null;
13         }
14         ListNode start = new ListNode(0); //学会保存链表的头
15         start.next = head;
16         ListNode slow = start;
17         ListNode fast = start;
18         for (int i = 0; i <= n; i++) {
19             fast = fast.next;
20         }
21         while (fast != null) {
22             slow = slow.next;
23             fast = fast.next;
24         }
25         slow.next = slow.next.next;
26         return start.next;
27     }
28 }

21. Merge Two Sorted Lists

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
11         if (l1 == null) {
12             return l2;
13         }
14         if (l2 == null) {
15             return l1;
16         }
17         ListNode newHead = null; //采用递归调用的思想
18         if (l1.val < l2.val) {
19             newHead = l1;
20             newHead.next = mergeTwoLists(l1.next, l2);
21         } else {
22             newHead = l2;
23             newHead.next = mergeTwoLists(l1, l2.next);
24         }
25         return newHead;
26     }
27 }

时间: 2024-10-12 01:57:40

LeetCode--链表的相关文章

leetcode 链表题总结

按照frequency来排序,总共27题 1.2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/#/description 两个链表相加,注意就是进位问题. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *

LeetCode链表相加-Python&lt;二&gt;

上一篇:LeetCode两数之和-Python<一> 题目:https://leetcode-cn.com/problems/add-two-numbers/description/ 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8

LeetCode链表解题模板

一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改,比如要计算链表的长度: ListNode* p = head; int num = 0; while(p!=NULL){ num++; p = p->next; } 如果要找到最后的节点,可以更改while循环中的条件,只不过需要加上head为NULL时的判断 if(!head) return hea

leetcode链表算法题实现思路

找出两个链表的交点(leetcode160) 一条链表遍历完之后跳转到下一条链表的头节点继续遍历.  因为当两条链表均被遍历一遍以后,第二次遍历时会同时到达节点相等的地方.如果没有相交的节点,两条链表均遍历两遍后,同时等于null,故返回null. public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode l1 = headA, l2 = headB; while (l1 != l2) { l1

LeetCode 链表题 ( Java )

leetcode 237. 删除链表中的节点 链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 示例 : 输入: head = [4,5,1,9], node = 5输出: [4,1,9]解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 这道题比较简单,修改之前节点的 next 指针,使其指向之后的节点: /** * Definition for sin

Leetcode 链表 Linked List Cycle II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle II Total Accepted: 20444 Total Submissions: 66195My Submissions Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you

leetcode 链表 Partition List

Partition List Total Accepted: 19761 Total Submissions: 73252My Submissions Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order

leetcode链表--16、swap-nodes-in-pairs(成对交换链表结点)

题目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given1->2->3->4, you should return the list as2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list,

leetcode链表--14、add-two-numbers(两链表相加 得到新链表)

题目描述 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -

leetcode链表--11、partition-list(链表分区)

题目描述 Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given1->4->3-&