LeetCode 328. Odd Even Linked List

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjxxyz123/article/details/79970408


  • 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 complexity and O(nodes) time complexity.
  • Example:
    Given 1->2->3->4->5->NULL,
    return 1->3->5->2->4->NULL.
  • Note:
    The relative order inside both the even and odd groups should remain as it was in the input.
    The first node is considered odd, the second node even and so on …
  • 地址


  • 做法类似于LeetCode 86. Partition List同样是分成两部分(虚拟出两条链表),同样是保持原来顺序(从前到后遍历),同样是 in place 。


  • 自己
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
class Solution {
    public ListNode oddEvenList(ListNode head) {
        if (head == null || head.next == null || head.next.next == null) {
            return head;
        ListNode oddHead = head;
        ListNode oddTail = oddHead;
        ListNode evenHead = head.next;
        ListNode evenTail = evenHead;
        ListNode curNode = head.next.next;
        boolean isOdd = true;
        while (curNode != null) {
            if (isOdd) {
                oddTail.next = curNode;
                oddTail = curNode;
            }else {
                evenTail.next = curNode;
                evenTail = curNode;
            isOdd = ! isOdd;
            curNode = curNode.next;
        oddTail.next = evenHead;
        evenTail.next = null;
        return head;
public class Solution {
    public ListNode oddEvenList(ListNode head) {
        if (head == null) return null;
        ListNode odd = head, even = head.next, evenHead = even;
        while (even != null && even.next != null) {
            odd.next = even.next;
            odd = odd.next;
            even.next = odd.next;
            even = even.next;
        odd.next = evenHead;
        return head;

two problems about linked list


who can solve these two problems?try these if you have the knowledge of data structure for c++.solve it in c++,please. rnrnProblem 1 .rnImplement a sorting algorithm using a singly linked list. Thernalgorithm (known as the Insertion sort algorithm) works as follows:rn1. It starts with an empty linked list.rn2. When a first node is inserted to the list, the node is inserted at the head of the list.rn3. To insert a new node, the algorithm traverses the linked list until it finds a specificrnposition to insert the new node by comparing the new node’s data value to eachrnexisting node’s data value.rn4. Traverses all the elements of the linked list to print out the sorted values. [optional]rn5. Repeats steps 3 and 4 for each and every new node.rnImplement the insertion sort algorithm in C++ to ascendingly sort integers. Your programrnshould receive an arbitrary number of input integers one by one until you interrupt or stoprnyour program (by pressing Ctrl-C, for example). For each input integer, the program printsrnout the sorted list after inserting the value. The screen shot of your program may look like thernfollowing:rn$ a.outrn== Insertion sort by [your name] ==rn[Empty List]rnInput an integer: 5rnSorted List : 5rnInput an integer: 7rnSorted List: 5 7rnInput an integer: 2rnSorted List: 2 5 7rnInput an integer: 6rnSorted List: 2 5 6 7rnInput an integer: 3rnSorted List: 2 3 5 6 7rnAlso answer the following questions (as the comments at the end of your C++ program) :rn� Explain the input sequences that will result in the best and the worst algorithmicrnperformances, in terms of the number of comparisons required.rn� If there are N input values to be sorted, what is the best case complexity and what is thernworse case complexity, in terms of N ?rn rnrnProblem 2 rn Implement the doubly circular linked list for integers in C++. Provide the following DCLL operations as the method members of the DCLL class :rn1. Add a node at the head of the listrn2. Add a node at the tail of the list (i.e. at head->previous)rn3. Add a node after a specific n-th node (e.g. after the 4-th node). If such node does notrnexist, simply deny the operationrn4. Delete a node from the head of the listrn5. Delete a node from the tail of the listrn6. Delete a specific n-th node (e.g. delete the 5-th node). If such node does not exist, simplyrndeny the operation.rn7. Delete all the nodes that have a specific value (i.e. yes, there can be duplicate nodes)rn8. Determine if the list contain a particular value and how many duplicate nodes there arern9. Traverse the list in a forward direction (i.e. from head to tail)rn10. Traverse the list in a backward direction (i.e. from tail to head)rnCreate a user interface in the main() function that is similar to the singly linked list (sll.cc)rnexample given on the class web page. The user interface should provide the above DCLLrnoperations appearing in such order. After applying each operation, your DCLL must stillrnremain consistent, i.e. all the pointers point to where they are supposed to.rn


  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他