白菜刷LeetCode记-328. Odd Even Linked List

发现简单题越来越少了,想偷懒都不可以了,今天的题目是中等难度的题目,题目如下:

这个题目是要根据链表的位置来修改链表,位置为奇数的节点全部排到前面,位置为偶数的节点全部排到奇数的后面,并且保持顺序不变。

想到的解决步骤为:

1、遍历数组,奇数的位置的节点组成一条新链表,偶数位置的节点组成另一个新链表;

2、将偶数链表接在奇数链表后面。

代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * function ListNode(val) {
 4  *     this.val = val;
 5  *     this.next = null;
 6  * }
 7  */
 8 /**
 9  * @param {ListNode} head
10  * @return {ListNode}
11  */
12 var oddEvenList = function(head) {
13     let idx = head;
14     let head1 = new ListNode(0);
15     let head2 = new ListNode(0);
16     let tail1 = head1;
17     let tail2 = head2;
18
19     let count = 0;
20     while(idx){
21         if(count%2 == 0){
22             tail1.next = idx;
23             idx = idx.next;
24             tail1 = tail1.next;
25             tail1.next = null;
26         }else{
27             tail2.next = idx;
28             idx = idx.next;
29             tail2 = tail2.next;
30             tail2.next = null;
31         }
32         count++;
33     }
34
35     head = head1.next;
36     tail1.next = head2.next;
37
38     return head;
39 };

END

原文地址:https://www.cnblogs.com/sssysukww/p/9668270.html

时间: 2024-10-08 21:05:14

白菜刷LeetCode记-328. Odd Even Linked List的相关文章

<LeetCode OJ> 328. Odd Even Linked List

328. Odd Even Linked List My Submissions Question Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node numbe

Leetcode刷题总结: 328. Odd Even Linked List

题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in place. The program should run in O(1) space comp

【Leetcode】 328. Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in place. The program should run in O(1) space complexi

白菜刷LeetCode记-811.Subdomain Visit Count

好久没有写LeetCode,所以说坚持真的是一件很难的事情啊.今日重新开始吧,先来一道简单的题目,如下: 这道题首先想到的还是使用Map,代码如下: /** * @param {string[]} cpdomains * @return {string[]} */ var subdomainVisits = function(cpdomains) { let tmp = new Map(); let res = new Array(); for(let i = 0 ; i < cpdomains

白菜刷LeetCode记-46. Permutations

今天这一题也是中等难度,题目如下: 这一题是要实现数组的全排列.这一题是要使用遍历以及递归的思想去实现,代码如下: 1 /** 2 * @param {number[]} nums 3 * @return {number[][]} 4 */ 5 var permute = function(nums) { 6 var res = new Array(); 7 8 helper(nums, 0, nums.length - 1 , res); 9 10 return res; 11 }; 12 1

白菜刷LeetCode记-122. Best Time to Buy and Sell Stock II

今天题目如下: 要求出最大的利益.这题个人不太想得通,看了答案也不太知道为什么这样能获得最大值.代码如下: 1 /** 2 * @param {number[]} prices 3 * @return {number} 4 */ 5 var maxProfit = function(prices) { 6 let maxp = 0; 7 for(let i = 0 ; i < prices.length ; i++){ 8 if(prices[i-1] < prices[i]){ 9 maxp

白菜刷LeetCode记-384. Shuffle an Array

今天早上是一道中等难度的题目,考的是洗牌算法. 个人对洗牌算法还是比较不熟悉的,因此是看答案的.参考链接为:https://www.jianshu.com/p/44100741cef5 基本思路为: 1) 将第一个元素与 n 个元素中的任意一个交换: 2) 将第二个与 n - 1 个元素进行交换: 3) 重复上述步骤,直到剩下1个元素. 代码如下: 1 var original; 2 var copy; 3 var num; 4 /** 5 * @param {number[]} nums 6

白菜刷LeetCode记-350. Intersection of Two Arrays II

今天题目如下: 比较简单,代码如下: /** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number[]} */ var intersect = function(nums1, nums2) { let myMap = new Map(); let myArr = new Array(); nums1.forEach(function(item){ if(myMap.has(item)){ myMap.set(i

&amp;lt;LeetCode OJ&amp;gt; 328. Odd Even Linked List

328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in t